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.
Use inherits in QT to determine the origin of the drag and drop event