qtquick: Delete the frame visual item delayed

If we're in a QML event handler we can't delete the QML item,
it's a QML limitation. We still want to delete the dockwidgets now though
since all tests depend on that.
This commit is contained in:
Sergio Martins
2020-11-21 14:49:37 +00:00
parent ef8807bc09
commit a847a574ed
2 changed files with 24 additions and 5 deletions

View File

@@ -35,16 +35,31 @@ FrameQuick::FrameQuick(QWidgetAdapter *parent, FrameOptions options)
QQmlComponent component(Config::self().qmlEngine(),
QUrl(QStringLiteral("qrc:/kddockwidgets/private/quick/qml/Frame.qml")));
auto visualItem = static_cast<QQuickItem*>(component.create());
m_visualItem = static_cast<QQuickItem*>(component.create());
if (!visualItem) {
if (!m_visualItem) {
qWarning() << Q_FUNC_INFO << "Failed to create item" << component.errorString();
return;
}
visualItem->setProperty("frameCpp", QVariant::fromValue(this));
visualItem->setParentItem(this);
visualItem->setParent(this);
m_visualItem->setProperty("frameCpp", QVariant::fromValue(this));
m_visualItem->setParentItem(this);
m_visualItem->setParent(this);
}
FrameQuick::~FrameQuick()
{
{
const DockWidgetBase::List docks = dockWidgets();
// The QML item must be deleted with deleteLater(), has we might be currently with its mouse
// handler in the stack. QML doesn't support it being deleted in that case.
// So unparent it and deleteLater().
m_visualItem->setParent(nullptr);
m_visualItem->deleteLater();
qDeleteAll(docks);
}
}
DockWidgetModel *FrameQuick::dockWidgetModel() const

View File

@@ -18,6 +18,8 @@
#include <QAbstractListModel>
class QQuickItem;
namespace KDDockWidgets {
class DockWidgetModel;
@@ -31,6 +33,7 @@ class DOCKS_EXPORT FrameQuick : public Frame
Q_PROPERTY(DockWidgetModel* dockWidgetModel READ dockWidgetModel CONSTANT)
public:
explicit FrameQuick(QWidgetAdapter *parent = nullptr, FrameOptions = FrameOption::FrameOption_None);
~FrameQuick() override;
DockWidgetModel *dockWidgetModel() const;
protected:
@@ -50,6 +53,7 @@ Q_SIGNALS:
void tabTitlesChanged();
private:
QQuickItem *m_stackLayout = nullptr;
QQuickItem *m_visualItem = nullptr;
DockWidgetBase *m_currentDockWidget = nullptr;
DockWidgetModel *const m_dockWidgetModel;
QHash<DockWidgetBase *, QMetaObject::Connection> m_connections; // To make it easy to disconnect from lambdas