Cannot show modal dialog or block in QFileSystemModel::dropMimeData() or QTreeView::dropEvent() in Qt 6 during drag and drop
-
Description:
In Qt 5.15 it was possible to show a modal dialog (e.g., QMessageBox::question()) directly inside QFileSystemModel::dropMimeData() or in QTreeView::dropEvent() during a drag and drop operation to ask the user for confirmation before accepting the drop.
Since upgrading to Qt 6 (tested on Qt 6.8), attempting to show a modal dialog or block the event processing in these functions results in an application crash. This breaks the workflow for applications that need explicit user confirmation before accepting a drop.
Steps to reproduce:
- Override QTreeView::dropEvent().
- In this override, display a modal QMessageBox::question() dialog to confirm the drop.
- Run the application.
- Perform a drag and drop operation.
Expected behavior:
The dialog should appear.
Depending on the user response, the drop is accepted or rejected.Actual behavior:
The application crashes immediately when the dialog is shown during dropEvent().
Code snippet:
class MyTreeView : public QTreeView { public: using QTreeView::QTreeView; protected: void dropEvent(QDropEvent* event) override { if (event->mimeData()->hasUrls()) { if (QMessageBox::question(this, "title", "body") == QMessageBox::Yes) { QTreeView::dropEvent(event); } else { event->ignore(); } } else { event->ignore(); } } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); QFileSystemModel *model = new QFileSystemModel(); model->setRootPath(QDir::homePath()); model->setReadOnly(false); MyTreeView *view = new MyTreeView(); view->setModel(model); view->setRootIndex(model->index(QDir::homePath())); view->setDragEnabled(true); view->setAcceptDrops(true); view->setDropIndicatorShown(true); view->setWindowTitle("Drop Test Qt 6"); view->resize(800, 600); view->show(); return app.exec(); }
This application crashes using Qt 6.8 but not using Qt 5.15.2 on Windows.
Environment:
Qt Version: 6.8 (also occurs on 6.5)
OS: Windows 10 x32
Compiler: MSVC 2022Questions:
Do you have any recommendation or workaround for this issue?
How can this issue be avoided in Qt 6? -
@PuskasAlex said in Cannot show modal dialog or block in QFileSystemModel::dropMimeData() or QTreeView::dropEvent() in Qt 6 during drag and drop:
QTreeView::dropEvent(event);
Hi,
Delete QTreeView::dropEvent(event) and everything will work fine:
if (QMessageBox::question(this, "title", "body") == QMessageBox::Yes)
event->accept();;
else
event->ignore();