Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

StackView.pop() from Item on Stack



  • Hello there,

    I have a problem with the StackView.
    What I want to do is, to pop an Item from the stack triggered inside the item on the stack.

    Example:
    main.qml:

    import QtQuick 2.4
    import QtQuick.Controls 1.4
    
    Item {
      id: appWidget
      width: 800
      height: 400
    
      ListModel {
        id:availableApps
    
        ListElement{
          name: "App1"
          app: "App1/App1.qml"
        }
      }
    
      GridView {
        id: appsGrid
        anchors.fill: parent
        anchors.margins: 20
        clip: true
        cellHeight: appsGrid.height/3
        cellWidth: appsGrid.width/5
        model: availableApps
        delegate: AppButton {onClicked: appStack.push(Qt.resolvedUrl(app))} // when clicked -> push view
      }
    
      StackView {
        id: appStack
        width: parent.width
        height: parent.height
      }
    }
    

    AppButton.qml

    import QtQuick 2.4
    
    Item {
      id: appButton
      width: GridView.view.cellWidth
      height: GridView.view.cellHeight
    
      signal clicked
    
      Text {
        id: appButtonLabel
        width: parent.width
    
        anchors.top: appButtonImage.bottom
        horizontalAlignment: Text.AlignHCenter
    
        font.pointSize: 14
    
        text: qsTr(name)
      }
    
      MultiPointTouchArea {
        width: parent.width
        height: parent.height
    
        onReleased: appButton.clicked()
      }
    }
    

    App1.qml

    import QtQuick 2.4
    
    Item {
      id:alignment
      height: parent.height
      width: parent.width
    
      Rectangle {
        id: rec
        anchors.fill: parent
        color: "yellow"
      }
    
      MultiPointTouchArea {
        anchors.fill: parent
    
        onReleased: rec.color = "orange" // here I want to pop the item from the appStack
      }
    }
    

    I tried following things:

    • call pop() from within the Item via Stack.view.pop() as the Item knows, it is on a Stack, but that is not possible ...
    • emit a signal and than handle it with currentItem.onSignal in the StackView but that also didn't work.
    • I also tried if (appStack) appStack.pop() inside the item

    As the rec.color = "orange" is working, I don't think its an focus problem.

    The application shall later run on a touchscreen device without a keyboard.



  • how did you capture a signal from StackView?



  • You mean from the Item on the Stack? I didn't, because it isn't possible or at least I don't know how.
    What I had done is, that I tried to capture it, but didn't found a way to do it properly.

    This is how I tried to emit the signal:
    In App1.qml I've added signal clicked that was called instead of the color change with alignment.clicked()

    This is how I tried to capture it:
    In the StackView declaration I tried to call currentItem.onClicked: appStack.pop(),currentItem.item.onClicked: appStack.pop() and currentItem.Stack.view.pop() as I have seen somewhere but nether it of was accepted as valid qml code ...



  • So, after playing around and a another few looks into the qt touch-gallery example, where they do exactly the with on of their buttons, I found a working solution and maybe a bug in the StackView.
    But thats I'm not really sure of. Maybe someone with more experience in QtQuick and StackView can confirm it ;)

    After adding a Text in App1 that displays the current stackdepth, I was surprised it shows a value of 2 even when I've only added one Item.
    And from this visibility of the depth, I was able to see that the call if (appStack) appStack.pop() call at onReleased: in App1 is actually working. The last Item was popped so that that there was only one left on the stack. As those were both yellow I havn't seen the pop effect before.

    What I'm not sure about now:

    • As I havn't found a rule, that a StackViewalways needs a initialItem, is it working as intended, that the first item to be added on a empty StackView is added twice?
    • Is it working as intended, that the last item on the Stackview can't be popped with pop() but must be popped with clear() ?

    If anyone with more experience in QtQuick and the usage of StackView knows anything about this points, feel free to explain your knowledge/thougths.

    If this is really a bug in StackView I would report it after confirmation ;)

    PS: as a workaround for now I'm using onReleased: appStack.depth > 1 ? appStack.pop() : appStack.clear() in App1.
    Like this both instances on the Stack are popped at the same time.


Log in to reply