[Qt-qml] Column has unexpected width

John Temples qt-qml at xargs.com
Mon Sep 26 21:10:06 CEST 2011


On Sat, 24 Sep 2011, Bo Thorsen wrote:

> Den 23-09-2011 20:14, John Temples skrev:
>> In the code below, there are two Columns, with the second Column
>> anchored to the right of the first one.  The first Column ends up with
>> a width of -1, and the second Column has an x of -1.  Can someone
>> explain why this happens?  I would have thought the Item would assume
>> the size of its child Rectangle and wouldn't need to have a size
>> explicitly provided.
>
> Things are not laid out automatically in QML, so what you are doing here
> is that you get random values for the two columns. At the very least,
> you need to set the anchors at the sides of the columns to the rectangle.

I tried anchoring the left/right of the Rectangles to the Columns, but
that had a no effect.  I've made a somewhat simpler version that
eliminates the Item; this code doesn't display either Rectangle.  If I
remove the anchors from the Rectangles, the Rectangles both appear in
the expected position:

import QtQuick 1.0

Rectangle {
     width: 800
     height: 480

     Column {
         id: column_1
         height: parent.height

         Rectangle {
             anchors.left: column_1.left
             anchors.right: column_1.right
             height: 50;
             width: 50;
             color: "red"
         }

         // width = -1
         Component.onCompleted: console.debug("width =", width)
     }

     Column {
         id: column_2
         anchors.left: column_1.right

         Rectangle {
             anchors.left: column_2.left
             anchors.right: column_2.right
             height: 50
             width:50
             color: "blue"
         }

         // x = -1
         Component.onCompleted: console.debug("x =", x, "width =",
         width)
     }
}

--
John W. Temples, III


More information about the Qt-qml mailing list