QmlDesigner.ItemLibrary: Start drag using QTimer::singleShot()

A drag might change the imports which invalidates the model exposed
to qml. Since QObjects are deleted when the model is reseted this did lead to
a crash in the qml engine, because the object emitting the signal was deleted.
To avoid this we call startDragAndDrop() asynchronously using QTimer::singleShot().
Now the object is always deleted after the signal is emitted not during the emit.

Task-number: QTCREATORBUG-11139

Change-Id: I92e7f9dfff33697b9ff0da148dc9da629723ab7e
Reviewed-by: Marco Bubke <marco.bubke@digia.com>
This commit is contained in:
Thomas Hartmann
2014-01-06 15:20:54 +01:00
parent 86a91be426
commit 1178a048fc
2 changed files with 16 additions and 6 deletions

View File

@@ -48,6 +48,7 @@
#include <QWheelEvent>
#include <QMenu>
#include <QApplication>
#include <QTimer>
#include <QQuickItem>
@@ -64,7 +65,8 @@ ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) :
m_resIconSize(24, 24),
m_itemsView(new QQuickView()),
m_resourcesView(new Internal::ItemLibraryTreeView(this)),
m_filterFlag(QtBasic)
m_filterFlag(QtBasic),
m_itemLibraryId(-1)
{
Internal::registerQmlTypes();
@@ -91,7 +93,7 @@ ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) :
QQuickItem *rootItem = qobject_cast<QQuickItem*>(m_itemsView->rootObject());
connect(rootItem, SIGNAL(itemSelected(int)), this, SLOT(showItemInfo(int)));
connect(rootItem, SIGNAL(itemDragged(int)), this, SLOT(startDragAndDrop(int)));
connect(rootItem, SIGNAL(itemDragged(int)), this, SLOT(startDragAndDropDelayed(int)));
connect(this, SIGNAL(scrollItemsView(QVariant)), rootItem, SLOT(scrollView(QVariant)));
connect(this, SIGNAL(resetItemsView()), rootItem, SLOT(resetView()));
@@ -363,12 +365,18 @@ void ItemLibraryWidget::setResourcePath(const QString &resourcePath)
updateSearch();
}
void ItemLibraryWidget::startDragAndDrop(int itemLibId)
void ItemLibraryWidget::startDragAndDropDelayed(int itemLibraryId)
{
QMimeData *mimeData = m_itemLibraryModel->getMimeData(itemLibId);
m_itemLibraryId = itemLibraryId;
QTimer::singleShot(0, this, SLOT(startDragAndDrop()));
}
void ItemLibraryWidget::startDragAndDrop()
{
QMimeData *mimeData = m_itemLibraryModel->getMimeData(m_itemLibraryId);
QDrag *drag = new QDrag(this);
drag->setPixmap(m_itemLibraryModel->getIcon(itemLibId).pixmap(32, 32));
drag->setPixmap(m_itemLibraryModel->getIcon(m_itemLibraryId).pixmap(32, 32));
drag->setMimeData(mimeData);
drag->exec();

View File

@@ -94,7 +94,8 @@ public slots:
void setResourcePath(const QString &resourcePath);
void startDragAndDrop(int itemLibId);
void startDragAndDropDelayed(int itemLibId);
void startDragAndDrop();
void showItemInfo(int itemLibId);
void setModel(Model *model);
@@ -137,6 +138,7 @@ private:
QWeakPointer<Model> m_model;
FilterChangeFlag m_filterFlag;
int m_itemLibraryId;
};
}