QQuickWindow: fix content item size
authorJ-P Nurmi <jpnurmi@digia.com>
Fri, 21 Feb 2014 14:04:21 +0000 (15:04 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Fri, 28 Feb 2014 13:32:47 +0000 (14:32 +0100)
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 <aalpert@blackberry.com>
Reviewed-by: Mitch Curtis <mitch.curtis@digia.com>
Reviewed-by: Gunnar Sletta <gunnar.sletta@jollamobile.com>
Reviewed-by: Jan Arve Sæther <jan-arve.saether@digia.com>
src/quick/items/qquickwindow.cpp
src/quick/items/qquickwindow_p.h
tests/auto/quick/qquickwindow/tst_qquickwindow.cpp

index 4eb41a2..bbc4e8b 100644 (file)
@@ -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<QObject> QQuickWindowPrivate::data()
 {
-    initContentItem();
     return QQmlListProperty<QObject>(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.
index 50f4a28..8b6073c 100644 (file)
@@ -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<QQuickItem *> parentlessItems;
index 73e45fa..3735947 100644 (file)
@@ -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<QQuickItem *>(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"