Add Window.window attached property

The Window attached property exposes all kinds of window attributes,
but not the window itself. Being able to access the window is often
useful for various purposes. For example, in QML TestCase, to be able
to access the window of the TestCase so that one can call various
slots such as requestActivate().

Change-Id: Id03c9f277bb17810b41a60957011ccf07399e149
Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
This commit is contained in:
J-P Nurmi 2016-01-31 09:39:34 +01:00
parent 20a51e87fd
commit 04d4dca69d
5 changed files with 27 additions and 4 deletions

View File

@ -3871,6 +3871,14 @@ void QQuickWindow::resetOpenGLState()
that you set if the requested flags could not be fulfilled.
*/
/*!
\qmlattachedproperty Window Window::window
\since 5.7
This attached property holds the item's window.
The Window attached property can be attached to any Item.
*/
/*!
\qmlattachedproperty int Window::width
\qmlattachedproperty int Window::height

View File

@ -51,9 +51,9 @@ QQuickWindowAttached::QQuickWindowAttached(QObject* attachee)
{
m_attachee = qobject_cast<QQuickItem*>(attachee);
if (m_attachee && m_attachee->window()) // It might not be in a window yet
windowChanged(m_attachee->window());
windowChange(m_attachee->window());
if (m_attachee)
connect(m_attachee, &QQuickItem::windowChanged, this, &QQuickWindowAttached::windowChanged);
connect(m_attachee, &QQuickItem::windowChanged, this, &QQuickWindowAttached::windowChange);
}
QWindow::Visibility QQuickWindowAttached::visibility() const
@ -86,7 +86,12 @@ int QQuickWindowAttached::height() const
return (m_window ? m_window->height() : 0);
}
void QQuickWindowAttached::windowChanged(QQuickWindow *window)
QQuickWindow *QQuickWindowAttached::window() const
{
return m_window;
}
void QQuickWindowAttached::windowChange(QQuickWindow *window)
{
if (window != m_window) {
QQuickWindow* oldWindow = m_window;
@ -95,6 +100,8 @@ void QQuickWindowAttached::windowChanged(QQuickWindow *window)
if (oldWindow)
oldWindow->disconnect(this);
emit windowChanged();
if (!oldWindow || !window || window->visibility() != oldWindow->visibility())
emit visibilityChanged();
if (!oldWindow || !window || window->isActive() != oldWindow->isActive())

View File

@ -69,6 +69,7 @@ class Q_AUTOTEST_EXPORT QQuickWindowAttached : public QObject
Q_PROPERTY(QQuickItem* contentItem READ contentItem NOTIFY contentItemChanged)
Q_PROPERTY(int width READ width NOTIFY widthChanged)
Q_PROPERTY(int height READ height NOTIFY heightChanged)
Q_PROPERTY(QQuickWindow *window READ window NOTIFY windowChanged)
public:
QQuickWindowAttached(QObject* attachee);
@ -79,6 +80,7 @@ public:
QQuickItem* contentItem() const;
int width() const;
int height() const;
QQuickWindow *window() const;
Q_SIGNALS:
@ -88,9 +90,10 @@ Q_SIGNALS:
void contentItemChanged();
void widthChanged();
void heightChanged();
void windowChanged();
protected Q_SLOTS:
void windowChanged(QQuickWindow*);
void windowChange(QQuickWindow*);
private:
QQuickWindow* m_window;

View File

@ -9,6 +9,7 @@ Rectangle {
property Item contentItem: root.Window.contentItem
property int windowWidth: root.Window.width
property int windowHeight: root.Window.height
property var window: root.Window.window
Text {
objectName: "rectangleWindowText"
anchors.centerIn: parent
@ -26,6 +27,7 @@ Rectangle {
property Item contentItem: Window.contentItem
property int windowWidth: Window.width
property int windowHeight: Window.height
property var window: Window.window
}
}
}

View File

@ -2033,6 +2033,7 @@ void tst_qquickwindow::attachedProperty()
QCOMPARE(view.rootObject()->property("contentItem").value<QQuickItem*>(), view.contentItem());
QCOMPARE(view.rootObject()->property("windowWidth").toInt(), view.width());
QCOMPARE(view.rootObject()->property("windowHeight").toInt(), view.height());
QCOMPARE(view.rootObject()->property("window").value<QQuickView*>(), &view);
QQuickWindow *innerWindow = view.rootObject()->findChild<QQuickWindow*>("extraWindow");
QVERIFY(innerWindow);
@ -2045,11 +2046,13 @@ void tst_qquickwindow::attachedProperty()
QCOMPARE(text->property("contentItem").value<QQuickItem*>(), innerWindow->contentItem());
QCOMPARE(text->property("windowWidth").toInt(), innerWindow->width());
QCOMPARE(text->property("windowHeight").toInt(), innerWindow->height());
QCOMPARE(text->property("window").value<QQuickWindow*>(), innerWindow);
text->setParentItem(0);
QVERIFY(!text->property("contentItem").value<QQuickItem*>());
QCOMPARE(text->property("windowWidth").toInt(), 0);
QCOMPARE(text->property("windowHeight").toInt(), 0);
QVERIFY(!text->property("window").value<QQuickWindow*>());
}
class RenderJob : public QRunnable