4

I'm using a RowLayout with 2 children in it. If I hide one of them, I want the other child to fill up the complete area. And when unhide it, the area should be evenly divided.

Problem I'm having is when 1 child is hidden at startup, and visibility changes at runtime. The area stays filled with only 1 child (and small bar is shown for spacing). While I expected that both childs would be visible.

Example:

RowLayout {
    anchors.fill: parent

    Rectangle {
        id: rect1
        Layout.fillWidth: true
        Layout.fillHeight: true
        color: "red"

        MouseArea
        {
            anchors.fill: parent
            onClicked:
            {
                rect2.visible = !rect2.visible
            }
        }
    }

    Rectangle {
        id: rect2
        Layout.fillWidth: true
        Layout.fillHeight: true
        color: "blue"
        visible: false
    }
}

If I remove the visible: false from rect2, it works expected. Only I want rect2 to be hidden at startup.

Putting rect2.visible = false in Component.onCompleted only adds extra code which imho should not be needed.

Any solution for this issue?

jpnurmi
  • 5,716
  • 2
  • 21
  • 37
RvdK
  • 19,580
  • 4
  • 64
  • 107
  • I think you are hitting a common "bug" here. See [this question](http://stackoverflow.com/q/34854337/2538363) and the related answer. – BaCaRoZzo Jun 23 '16 at 10:33
  • The answer there is only a small code example to show the issue. Actual solution is filing a bug report and hope it will be fixed? Any other ways to do this? – RvdK Jun 23 '16 at 10:46
  • Using `preferredWidth: parent.width / 2` should be the solution right now. – BaCaRoZzo Jun 23 '16 at 10:47
  • @BaCaRoZzo: for this indeed, defeating the purpose of using the Layout itself. In my project, I have 8+ items, which indepently should be visible or not. Simply dividing by 8 won't do it. I would then count the amount of visible ones and use that. – RvdK Jun 23 '16 at 12:10
  • 2
    I should have been clearer, sorry. The point is `fillwidth` rules over other rules. The bug is related to the fact that, for some reasons, the lack of a `preferredWidth` (or does it suffice `minimumWidth` for instance? Didn't test) does lead to the bug. Without further babbling just try out [this 2 mins example](http://pastebin.com/embed_iframe/M3N0T9Ly). Also, it seems the feature (still seeing it as a bug...) is scheduled [here](https://bugreports.qt.io/browse/QTBUG-45781). – BaCaRoZzo Jun 23 '16 at 12:36
  • Hmm cool it works, doesn't matter what preferredWidth is, as long it's the same value everywhere. – RvdK Jun 23 '16 at 12:50
  • My painful experience: NEVER use `RowLayout` , use `Row` instead. Don't know why `Layout` usually cannot adjust size after any of child's size-changing. – kuanyui Oct 13 '16 at 06:58

0 Answers0