1

I want to instantiate items and then display them using a StackView, via

MyItem0
{
    id: item0
}

MyItem1
{
    id: item1
}

myStackView.push(item0)
...
myStackView.pop()
myStackView.push(item1)

The problem is that when I create items item0 and item1, they are all shown, which is obviously not what I want.

I guess it somehow relates to this post.

Any idea?

arennuit
  • 855
  • 1
  • 7
  • 23

2 Answers2

3

You can implement the items as properties rather than children, which will result in them not showing until they are pushed on the stack view:

property Item item0: MyItem0 { }
property Item item1: MyItem1 { }

However, if you pop, the item will remain visible, as its parent will no longer be null.

pop() is said to return the popped item, so pop().parent = null should do the trick, but for me for some reason it returns a null.

So instead you can simply set the parent explicitly to null:

  pop()
  item0.parent = null

Naturally, you can just as well set the visible or opacity properties. Which may be preferable, as even more funky and illogical behavior surfaces. if you try to push the same item again after it has been pushed and popped once, it doesn't work, and there is an nothing to push error message in the debug console.

Edit: OK, regarding the last paragraph, it seems that whatever method used, once an object has been pushed and popped, the same object cannot be pushed again, even if the parent is not set to null. This looks like a bug.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • I observe the same behavior, this is a pain. – arennuit Jul 27 '17 at 16:11
  • This is a perfect example of why I stay away from using stock controls. They work very well when you only limit yourself to "by the book" usage, but the moment you try to do something more free-style and they epically fail. I am like "I am going to build my own stack view, with blackjack, and hookers" :) – dtech Jul 27 '17 at 16:13
  • can you let me know which version of Qt you are using? – arennuit Jul 27 '17 at 16:35
  • Sure - latest 5.9.1 – dtech Jul 27 '17 at 16:39
  • @dtech regarding your last edit: I think you are stuck to `item0`, which you can't push again, since you have not removed it from the stack. `pop()` will leave the `initialItem` (the lowest from the stack) on the stack. To remove it, you need to call `clear()` – derM - not here for BOT dreams Jul 27 '17 at 17:20
  • `pop()` should remove the top item, that is the last item pushed, which is `item0`. – dtech Jul 27 '17 at 19:36
  • OK, evidently you cannot pop the last item to make the stack empty, still doesn't make sense, but that's the way it is. – dtech Jul 28 '17 at 00:15
3

Create them with no parent:

import QtQuick 2.5
import QtQuick.Controls 2.0
import QtQuick.Window 2.0

ApplicationWindow {
    id: window
    visible: true
    width: 600
    height: 600



    StackView {
        anchors.fill: parent
        id: sv
        initialItem: Button {
            id: b
            onClicked: sv.push(b1)
            text: "0"
        }
    }

    Button {
        id: b1
        parent: null
        onClicked: sv.push(b2)
        text: "1"
        x: 100
        onParentChanged: console.log('parent', parent, 1)
        onVisibleChanged: console.log('visible', visible, 1)
    }

    Button {
        id: b2
        parent: null
        onClicked: sv.push(b3)
        text: "2"
        x: 200
        onParentChanged: console.log('parent', parent, 2)
        onVisibleChanged: console.log('visible', visible, 2)
    }

    Button {
        id: b3
        parent: null
        onClicked: sv.pop()
        text: "3"
        x: 300
        onParentChanged: console.log('parent', parent, 3)
        onVisibleChanged: console.log('visible', visible, 3)
    }


    Button {
        id: back
        onClicked: sv.pop()
        text: 'pop'
        Binding {
            target: back.background
            property: 'color'
            value: 'orange'
        }
    }

    Button {
        id: clear
        x: 150
        onClicked: sv.clear()
        text: 'clear'
        Binding {
            target: clear.background
            property: 'color'
            value: 'orange'
        }
    }
}

In QtQuick.Controls 2.x the will be reparented to null when you pop() them, effectively hiding them.

In QtQuick.Controls 1.x they will be reparented to some ContentItem but their visible property will be set to false, hiding them aswell.

To test it, just change line 2 to import QtQuick.Controls 1.4

Remember: pop() will leave the item with index 0 on the stack. To remove that, call clear()