From 0b2d05093c6fc030d40be8c873ad1e0af9bba8cc Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 21 Feb 2014 15:04:21 +0100 Subject: [PATCH] QQuickWindow: fix content item size MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Resize content item in QQuickWindow::resizeEvent() instead of using signals and slots. The signal-slot connections were only established when child items were added in QML. It should work in C++ too, since QQuickWindow and QQuickItem are part of the public C++ API and using them in C++ is a perfectly valid use case. Resizing the content item in resizeEvent() is not only faster than using signals and slots, but also in theory a bit more flexible as one would be able to override the event handler and implement their own layouting. Task-number: QTBUG-36938 Change-Id: Id05d4cf6d547021803050633e6f0a3359129a9f3 Reviewed-by: Alan Alpert Reviewed-by: Mitch Curtis Reviewed-by: Gunnar Sletta Reviewed-by: Jan Arve Sæther --- src/quick/items/qquickwindow.cpp | 17 +++-------- src/quick/items/qquickwindow_p.h | 1 - tests/auto/quick/qquickwindow/tst_qquickwindow.cpp | 34 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/quick/items/qquickwindow.cpp b/src/quick/items/qquickwindow.cpp index 4eb41a2..bbc4e8b 100644 --- a/src/quick/items/qquickwindow.cpp +++ b/src/quick/items/qquickwindow.cpp @@ -216,9 +216,11 @@ void QQuickWindow::exposeEvent(QExposeEvent *) } /*! \reimp */ -void QQuickWindow::resizeEvent(QResizeEvent *) +void QQuickWindow::resizeEvent(QResizeEvent *ev) { Q_D(QQuickWindow); + if (d->contentItem) + d->contentItem->setSize(ev->size()); if (d->windowManager) d->windowManager->resize(this); } @@ -433,6 +435,7 @@ void QQuickWindowPrivate::init(QQuickWindow *c, QQuickRenderControl *control) contentItemPrivate->window = q; contentItemPrivate->windowRefCount = 1; contentItemPrivate->flags |= QQuickItem::ItemIsFocusScope; + contentItem->setSize(q->size()); customRenderMode = qgetenv("QSG_VISUALIZE"); renderControl = control; @@ -474,24 +477,12 @@ void QQuickWindowPrivate::init(QQuickWindow *c, QQuickRenderControl *control) QQmlListProperty QQuickWindowPrivate::data() { - initContentItem(); return QQmlListProperty(q_func(), 0, QQuickWindowPrivate::data_append, QQuickWindowPrivate::data_count, QQuickWindowPrivate::data_at, QQuickWindowPrivate::data_clear); } -void QQuickWindowPrivate::initContentItem() -{ - Q_Q(QQuickWindow); - q->connect(q, SIGNAL(widthChanged(int)), - contentItem, SLOT(setWidth(int))); - q->connect(q, SIGNAL(heightChanged(int)), - contentItem, SLOT(setHeight(int))); - contentItem->setWidth(q->width()); - contentItem->setHeight(q->height()); -} - static QMouseEvent *touchToMouseEvent(QEvent::Type type, const QTouchEvent::TouchPoint &p, QTouchEvent *event, QQuickItem *item, bool transformNeeded = true) { // The touch point local position and velocity are not yet transformed. diff --git a/src/quick/items/qquickwindow_p.h b/src/quick/items/qquickwindow_p.h index 50f4a28..8b6073c 100644 --- a/src/quick/items/qquickwindow_p.h +++ b/src/quick/items/qquickwindow_p.h @@ -105,7 +105,6 @@ public: virtual ~QQuickWindowPrivate(); void init(QQuickWindow *, QQuickRenderControl *control = 0); - void initContentItem();//Currently only used if items added in QML QQuickRootItem *contentItem; QSet parentlessItems; diff --git a/tests/auto/quick/qquickwindow/tst_qquickwindow.cpp b/tests/auto/quick/qquickwindow/tst_qquickwindow.cpp index 73e45fa..3735947 100644 --- a/tests/auto/quick/qquickwindow/tst_qquickwindow.cpp +++ b/tests/auto/quick/qquickwindow/tst_qquickwindow.cpp @@ -355,6 +355,8 @@ private slots: void animatingSignal(); + void contentItemSize(); + private: QTouchDevice *touchDevice; QTouchDevice *touchDeviceWithVelocity; @@ -1707,6 +1709,38 @@ void tst_qquickwindow::animatingSignal() QTRY_VERIFY(spy.count() > 1); } +// QTBUG-36938 +void tst_qquickwindow::contentItemSize() +{ + QQuickWindow window; + QQuickItem *contentItem = window.contentItem(); + QVERIFY(contentItem); + QCOMPARE(QSize(contentItem->width(), contentItem->height()), window.size()); + + QSizeF size(300, 200); + window.resize(size.toSize()); + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + + QCOMPARE(window.size(), size.toSize()); + QCOMPARE(QSizeF(contentItem->width(), contentItem->height()), size); + + QQmlEngine engine; + QQmlComponent component(&engine); + component.setData(QByteArray("import QtQuick 2.1\n Rectangle { anchors.fill: parent }"), QUrl()); + QQuickItem *rect = qobject_cast(component.create()); + QVERIFY(rect); + rect->setParentItem(window.contentItem()); + QCOMPARE(QSizeF(rect->width(), rect->height()), size); + + size.transpose(); + window.resize(size.toSize()); + QCOMPARE(window.size(), size.toSize()); + // wait for resize event + QTRY_COMPARE(QSizeF(contentItem->width(), contentItem->height()), size); + QCOMPARE(QSizeF(rect->width(), rect->height()), size); +} + QTEST_MAIN(tst_qquickwindow) #include "tst_qquickwindow.moc" -- 2.7.4