[Development] Inconsistent resizing of QQuickView (unexpected item’s width & height)

Juan Navarro oneorjuan at gmail.com
Tue Apr 15 15:24:57 CEST 2014


Hi all, I'm facing a problem when resizing a QQuickView instance and I'd
like to ask some developers if this issue is a bug or either an error on my
side
(Qt Project forums thread: http://qt-project.org/forums/viewthread/41445/)

I have a simple test case that produces different output when ran on
Windows and Linux:

C++:
    #include <QGuiApplication>
    #include <QQuickView>
    #include <QQuickItem>
    #include <QUrl>
    #include <QMetaObject>
    #include <QVariant>

    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);

        QQuickView *viewer = new QQuickView();
        viewer->setSource(QUrl::fromLocalFile("main.qml"));
        viewer->setResizeMode(QQuickView::SizeRootObjectToView);
        viewer->show();
        viewer->resize(500, 500);
        qApp->processEvents();
        // At this point, QML width, height and positioners already have
correct values

        QMetaObject::invokeMethod(viewer->rootObject(),
            "addTile", Q_ARG(QVariant, 3));

        return app.exec();
    }

QML:

    import QtQuick 2.0

    Rectangle {
        id: root

        property real gridItemSize: width * 0.2125

        function addTile(index) {
            console.log("[QML addTile]");
            console.log("width:", width, "height:", height);
            for (var i = 0; i < gridRepeater.count; i++) {
                console.log("Item", i,
                    "x:", gridRepeater.itemAt(i).x,
                    "y:", gridRepeater.itemAt(i).y);
            }

            var tile = Qt.createQmlObject(
                'import QtQuick 2.0; Rectangle { color: "red" }', root);
            tile.width = gridItemSize;
            tile.height = gridItemSize;
            tile.x = gridRepeater.itemAt(index).x;
            tile.y = gridRepeater.itemAt(index).y;
        }

        Grid {
            columns: 2

            Repeater {
                id: gridRepeater
                model: 6

                Rectangle {
                    width: gridItemSize; height: gridItemSize
                    border.color: "black"
                }
            }
        }

        Component.onCompleted: {
            console.log("[QML onCompleted]");
            console.log("width:", width, "height:", height);
            for (var i = 0; i < gridRepeater.count; i++) {
                console.log("Item", i,
                    "x:", gridRepeater.itemAt(i).x,
                    "y:", gridRepeater.itemAt(i).y);
            }
        }
    }


This program should show a 3x2 grid where the cell (1, 1) contains a red
square. The grid is always correctly drawn, but the red square only gets
drawn on Windows builds. These are each respective output logs, and the
info of each tested platform:

Windows 7 Service Pack 1 64bit; Visual Studio 2012 Update 4; Qt 5.2.1:
    [QML onCompleted]
    width: 0 height: 0
    Item 0 x: 0 y: 0
    Item 1 x: 0 y: 0
    Item 2 x: 0 y: 0
    Item 3 x: 0 y: 0
    Item 4 x: 0 y: 0
    Item 5 x: 0 y: 0
    [QML addTile]
    width: 500 height: 500
    Item 0 x: 0 y: 0
    Item 1 x: 106.25 y: 0
    Item 2 x: 0 y: 106.25
    Item 3 x: 106.25 y: 106.25
    Item 4 x: 0 y: 212.5
    Item 5 x: 106.25 y: 212.5

Kubuntu/Xubuntu 12.04.4; GCC v4.6.3; Qt 5.2.1 & Qt 5.1.1
    [QML onCompleted]
    width: 0 height: 0
    Item 0 x: 0 y: 0
    Item 1 x: 0 y: 0
    Item 2 x: 0 y: 0
    Item 3 x: 0 y: 0
    Item 4 x: 0 y: 0
    Item 5 x: 0 y: 0
    [QML addTile]
    width: 0 height: 0
    Item 0 x: 0 y: 0
    Item 1 x: 0 y: 0
    Item 2 x: 0 y: 0
    Item 3 x: 0 y: 0
    Item 4 x: 0 y: 0
    Item 5 x: 0 y: 0

Any hints on this behavior?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20140415/f42dcc99/attachment.html>


More information about the Development mailing list