From 2df18ec34f13ffb84fcb5918e834c9571fd99ce2 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Fri, 24 Mar 2017 10:44:33 +0100 Subject: [PATCH] QmlDesigner: Fix drag and drop The release mouse event does not come through if we start a drag. There was already a workaround that stopped working with 5.8 and did rely on private API. I removed all usage of private API and added a simple workaround that synthesises a mouse release event. The actual execution of the drag is now asynchronous to ensure the release event is delivered properly. I removed all dependencies on private API in the designer. In Qt 5.9 the issue seems to be fixed in Qt. The workaround does not seem to get in the way of the fix. Eventually, the workaround can be removed. Change-Id: I9b45b255da5e44c26aba2acf4a42f88537126f75 Reviewed-by: Tim Jenssen --- .../itemLibraryQmlSources/ItemDelegate.qml | 6 +---- .../itemlibrary/itemlibrarywidget.cpp | 23 ++++++++----------- .../itemlibrary/itemlibrarywidget.h | 2 +- src/plugins/qmldesigner/qmldesignerplugin.pro | 1 - 4 files changed, 11 insertions(+), 21 deletions(-) diff --git a/share/qtcreator/qmldesigner/itemLibraryQmlSources/ItemDelegate.qml b/share/qtcreator/qmldesigner/itemLibraryQmlSources/ItemDelegate.qml index 32c2f5a0c8f..09f4f6c8fe4 100644 --- a/share/qtcreator/qmldesigner/itemLibraryQmlSources/ItemDelegate.qml +++ b/share/qtcreator/qmldesigner/itemLibraryQmlSources/ItemDelegate.qml @@ -73,12 +73,8 @@ Item { id: mouseRegion anchors.fill: parent - property bool reallyPressed: false - property int pressedX - property int pressedY - onPressed: { - rootView.startDragAndDrop(itemLibraryEntry) + rootView.startDragAndDrop(mouseRegion, itemLibraryEntry) } } } diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp index 5b2dc1034c1..e9d1ab13ba7 100644 --- a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp +++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -55,10 +56,6 @@ #include #include -#include // mouse ungrabbing workaround on quickitems -#include // mouse ungrabbing workaround on quickitems - - namespace QmlDesigner { ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) : @@ -265,14 +262,7 @@ void ItemLibraryWidget::setResourcePath(const QString &resourcePath) updateSearch(); } -static void ungrabMouseOnQMLWorldWorkAround(QQuickWidget *quickWidget) -{ - const QQuickWidgetPrivate *widgetPrivate = QQuickWidgetPrivate::get(quickWidget); - if (widgetPrivate && widgetPrivate->offscreenWindow && widgetPrivate->offscreenWindow->mouseGrabberItem()) - widgetPrivate->offscreenWindow->mouseGrabberItem()->ungrabMouse(); -} - -void ItemLibraryWidget::startDragAndDrop(QVariant itemLibraryId) +void ItemLibraryWidget::startDragAndDrop(QQuickItem *mouseArea, QVariant itemLibraryId) { m_currentitemLibraryEntry = itemLibraryId.value(); @@ -283,9 +273,14 @@ void ItemLibraryWidget::startDragAndDrop(QVariant itemLibraryId) m_currentitemLibraryEntry.libraryEntryIconPath())); drag->setMimeData(mimeData); - drag->exec(); + /* Workaround for bug in Qt. The release event is not delivered for Qt < 5.9 if a drag is started */ + QMouseEvent event (QEvent::MouseButtonRelease, QPoint(-1, -1), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); + QApplication::sendEvent(mouseArea, &event); - ungrabMouseOnQMLWorldWorkAround(m_itemViewQuickWidget.data()); + QTimer::singleShot(0, [drag]() { + drag->exec(); + drag->deleteLater(); + }); } void ItemLibraryWidget::removeImport(const QString &name) diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.h b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.h index 946dfc9b7d0..a831b4c5fd1 100644 --- a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.h +++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.h @@ -84,7 +84,7 @@ public: void setModel(Model *model); - Q_INVOKABLE void startDragAndDrop(QVariant itemLibId); + Q_INVOKABLE void startDragAndDrop(QQuickItem *mouseArea, QVariant itemLibId); protected: void removeImport(const QString &name); diff --git a/src/plugins/qmldesigner/qmldesignerplugin.pro b/src/plugins/qmldesigner/qmldesignerplugin.pro index 460a77a96ef..a5261e0931a 100644 --- a/src/plugins/qmldesigner/qmldesignerplugin.pro +++ b/src/plugins/qmldesigner/qmldesignerplugin.pro @@ -1,5 +1,4 @@ QT += quickwidgets -QT += widgets-private quick-private quickwidgets-private core-private gui-private #mouse ungrabbing workaround on quickitems CONFIG += exceptions INCLUDEPATH += $$PWD