Add View::isNull()

ViewWrappers don't have ownership of the gui element.
The gui element might have been deleted, leaving us with a
stale wrapper

For impl simply use a QPointer in the QtWidgets and QtQuick
backends
This commit is contained in:
Sergio Martins
2022-04-05 21:49:06 +01:00
parent ad3febd87e
commit 43faf85eb4
6 changed files with 29 additions and 4 deletions

View File

@@ -258,3 +258,8 @@ bool View::equals(const std::shared_ptr<ViewWrapper> &other) const
{
return other && handle() == other->handle();
}
bool View::isNull() const
{
return false;
}

View File

@@ -93,6 +93,12 @@ public:
/// as long as it uniquely identifies the GUI element. KDDW backend only uses it for comparison purposes
virtual HANDLE handle() const = 0;
/// @brief Returns whether the gui item represented by this view was already deleted
/// Usually false, as KDDW internal gui elements inherit View, and nobody will access them after destruction.
/// However, ViewWrapper derived classes, wrap an existing gui element, which might get deleted.
/// Override isNull() in our ViewWrapper subclasses and return true if the wrapped gui element was already deleted
virtual bool isNull() const;
/// @brief Called by the layouting engine
/// Override it in case your widget needs to know where it is in the layout. Usually only needed by Frame.s
virtual void setLayoutItem(Layouting::Item *) {};

View File

@@ -168,7 +168,7 @@ std::shared_ptr<ViewWrapper> ViewWrapper_qtquick::parentView() const
HANDLE ViewWrapper_qtquick::handle() const
{
return reinterpret_cast<HANDLE>(m_item);
return reinterpret_cast<HANDLE>(m_item.data());
}
void ViewWrapper_qtquick::grabMouse()
@@ -192,3 +192,8 @@ QString ViewWrapper_qtquick::objectName() const
{
return {};
}
bool ViewWrapper_qtquick::isNull() const
{
return m_item.data() == nullptr;
}

View File

@@ -14,6 +14,7 @@
#include "ViewWrapper.h"
#include <QQuickItem>
#include <QPointer>
namespace KDDockWidgets::Views {
@@ -50,9 +51,10 @@ public:
QScreen *screen() const override;
void setFocus(Qt::FocusReason) override;
QString objectName() const override;
bool isNull() const override;
private:
QQuickItem *const m_item;
QPointer<QQuickItem> m_item;
};
}

View File

@@ -243,7 +243,7 @@ std::shared_ptr<ViewWrapper> ViewWrapper_qtwidgets::parentView() const
HANDLE ViewWrapper_qtwidgets::handle() const
{
return reinterpret_cast<HANDLE>(m_widget);
return reinterpret_cast<HANDLE>(m_widget.data());
}
void ViewWrapper_qtwidgets::grabMouse()
@@ -270,3 +270,8 @@ QString ViewWrapper_qtwidgets::objectName() const
{
return m_widget->QWidget::objectName();
}
bool ViewWrapper_qtwidgets::isNull() const
{
return m_widget.data() == nullptr;
}

View File

@@ -14,6 +14,7 @@
#include "ViewWrapper.h"
#include <QWidget>
#include <QPointer>
namespace KDDockWidgets::Views {
@@ -50,9 +51,10 @@ public:
QScreen *screen() const override;
void setFocus(Qt::FocusReason) override;
QString objectName() const override;
bool isNull() const override;
private:
QWidget *const m_widget;
QPointer<QWidget> m_widget;
};
}