Solved Detect Drag on Drop over all Child Widgets
-
Hello,
I'm trying to implement Drag and Drop behavior, across my whole application, I would like to execute some action when a drop occurs, no mater where in the Window . The Problem that I'm facing is that in my MainWindow, I have a Widget which contains a QGraphicsView, which again contains a QGraphicsScene, I can detect the drop anywhere in the application via EventFilter, except in the in the View and Scene. Is it possible to achieve some kind of global drag and drop behavior which I can detect from the MainWindow?
Regards,
Damir -
The issue was that the QGraphicsScene only accepts DragAndDrops over items, the solution for me was to overwrite the DragMoveEvent. I've posted my solution here https://stackoverflow.com/questions/61026334/detect-drag-on-drop-over-all-child-qwidgets/61073437#61073437
-
Hi,
How did you setup the drag and drop part of your application ?
-
Basically in my MainWindow I have this:
void MainWindow::dropEvent(QDropEvent *event) { auto pixmap = QPixmap(event->mimeData()->urls().first().toString().remove("file://")); if(!pixmap.isNull()) { load(pixmap); } event->acceptProposedAction(); } void MainWindow::dragEnterEvent(QDragEnterEvent *event) { if (event->mimeData()->hasUrls()) { event->acceptProposedAction(); } }
This worked only when the drag and drop happens over the MainWindow, but not over the Child Widget that contains the View and Scene. So I tried installing an event filter but it didn't work:
bool MainWindow::eventFilter(QObject *obj, QEvent *event) { if(event->type() == QEvent::DragEnter) { qDebug("Enter"); } else if(event->type() == QEvent::Drop) { qDebug("Drop"); } else if(event->type() == QEvent::GraphicsSceneDrop) { qDebug("Scene drop"); } else if (event->type() >= 159 && event->type() <= 168) { qDebug("Any Scene Event"); } return QObject::eventFilter(obj, event); }
This was installed on my Child Widget in the MainWindow constructor:
... setAcceptDrops(true); mChildWidget->installEventFilter(this); mChildWidget->setAcceptDrops(true); ...
Didn't capture anything over the View/Scene
-
Standard view and scene ? How did you set them up ?
-
@SGaist Nothing special I guess, for both I have my own classes that inherit from the Qt ones.
This is the Scene:
https://github.com/ksnip/kImageAnnotator/blob/master/src/annotations/core/AnnotationArea.h
https://github.com/ksnip/kImageAnnotator/blob/master/src/annotations/core/AnnotationArea.cppThis is the View:
https://github.com/ksnip/kImageAnnotator/blob/master/src/gui/annotator/AnnotationView.h
https://github.com/ksnip/kImageAnnotator/blob/master/src/gui/annotator/AnnotationView.cpp -
The issue was that the QGraphicsScene only accepts DragAndDrops over items, the solution for me was to overwrite the DragMoveEvent. I've posted my solution here https://stackoverflow.com/questions/61026334/detect-drag-on-drop-over-all-child-qwidgets/61073437#61073437