In Qt, inherits is used to determine the origin of the drag and drop event. inheritsdrag
Sometimes we needDragMoveEvent,DropEventSuch a function filters out invalid drag or distinguishes different types of drag for different processing. For example, the Code is roughly as follows:
void Scene::dragMoveEvent(QGraphicsSceneDragDropEvent *event){ if(event->source()********) { event->accept(); event->setDropAction(Qt::CopyAction); } else { QGraphicsScene::dragMoveEvent(event); }}
Here, event-> source returns a QWidget pointer, which does not have much type information. In this case, the common practice is to judge based on the pointer address.
if(event->source() == static_cast<QWidget *>(xxx)) { event->accept(); event->setDropAction(Qt::CopyAction); }
Okay, let's assume that drag is made on xxx. It's okay. If there are many xxx files like this, you can also store them in a list.
//QList<QWidget *> widgetLists; if(widgetLists.contains(<span style="font-family: Arial, Helvetica, sans-serif;">event->source()</span><span style="font-family: Arial, Helvetica, sans-serif;">))</span> { event->accept(); event->setDropAction(Qt::CopyAction); }
If you want to determine the type, you can install different types in different lists and then judge them accordingly. however, it is troublesome to save the pointer passed from other places. is there a better way?
There are actually some, you can use the inherits method:
if(event->source()->inherits("QToolButton"))
{
event->accept();
event->setDropAction(Qt::CopyAction);
}
In this way, it is very easy to determine the type. For example, pushButton drag is used for processing and ToolButton is used for processing.
Help QT inherits usage
Bool QObject: inherits (const char * lname) const
If this object is an instance of the class that inherits the clname and the lname inherits the QObject, the system returns true. Otherwise, the system returns false.
A class can be considered to inherit itself.
Instance:
QTimer * t = new QTimer; // QTimer inherits QObject
T-> inherits ("QTimer"); // return TRUE
T-> inherits ("QObject"); // return TRUE
T-> inherits ("QButton"); // return FALSE
// QScrollBar inherits QWidget and QRangeControl
QScrollBar * s = new QScrollBar (0 );
S-> inherits ("QWidget"); // return TRUE
S-> inherits ("QRangeControl"); // return FALSE
(Note: QRangeControl is not a QObject .)