QML StackView:pop上的项目已销毁

QML StackView:pop上的项目已销毁,qml,Qml,考虑以下示例: import QtQuick 2.4 import QtQuick.Controls 1.3 import QtQuick.Window 2.2 Window { visible: true width: 320 height: 320 StackView { id: stackView anchors.fill: parent Component.onCompleted: {

考虑以下示例:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2

Window {
    visible: true
    width: 320
    height: 320

    StackView {
        id: stackView
        anchors.fill: parent
        Component.onCompleted: {
            stackView.push( { item: comp1, destroyOnPop:false } )
        }
    }

    Component {
        id: comp1
        Rectangle {
            color: "lightgray"

            Text {
                id: mtext
                anchors.centerIn: parent
                text: "First Page"
            }
        }
    }

    Component {
        id: comp2
        Rectangle {
            color: "lightgreen"

            Text {
                id: mtext
                anchors.centerIn: parent
                text: "Second Page"
            }

            MouseArea {
                id: mouseArea
                anchors.fill: parent
                onClicked: mtext.text += " Clicked"
            }
        }
    }

    Row {
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        Button {
            id: next
            text: "Next"
            onClicked: {
                stackView.push( { item: comp2, destroyOnPop:false } )
                enabled = false
                prev.enabled = true
            }
        }

        Button {
            id: prev
            text: "Prev"
            enabled: false
            onClicked: {
                stackView.pop()
                enabled = false
                next.enabled = true
            }
        }
    }
}
在这里,我将2个
组件
s按入
堆栈视图
,单击每个按钮,即单击“下一步”按钮加载第二页。当我点击它时,它会显示一个更新的文本(“点击第二页”)。然后单击“Prev”按预期加载第一页

现在,如果我再次单击“下一步”,它应该加载第二个页面和更新的文本(“第二个页面已单击”),但它没有。它显示初始文本(“第二页”)

所以问题是
项目是否在pop上被销毁?如果不是,那么第二页是否应该显示更新的文本(“单击第二页”)

我甚至将标志
destroonpop
设置为
false
。我是否误解了StackView的概念?有什么办法可以解决这个问题吗


我想从
StackView
中的任意点加载任何页面,并确保
项的状态与我离开时的状态相同。就像
QStackWidget
一样,我们使用
setCurrentIndex()
文档是指
对象,而您使用的是
组件
s。组件是而不是
,因为它不是从
派生出来的,这一点非常清楚。实际上,
组件
的行为类似于实例化到特定对象的

每次你打电话

stackView.push( { item: comp2, destroyOnPop:false } )
生成组件
comp2
的新“实例”,并使用该新实例代替前一个实例。我强烈建议阅读以更好地理解
组件
对象和生成的实例之间的相关性

实例可以通过
createObject
函数生成。因此,您可以创建
s的数组,并使用这样的
s来代替
组件
s。最终代码如下所示:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2

Window {
    visible: true
    width: 320
    height: 320

    StackView {
        id: stackView
        anchors.fill: parent
        Component.onCompleted: {
            push( { item: items[0], destroyOnPop:false })
        }
        property variant items: [comp1.createObject(), comp2.createObject()]  // objects from the components
    }

    Component {
        id: comp1
        Rectangle {
            color: "lightgray"

            Text {
                id: mtext
                anchors.centerIn: parent
                text: "First Page"
            }
        }
    }

    Component {
        id: comp2
        Rectangle {
            color: "lightgreen"

            Text {
                id: mtext
                anchors.centerIn: parent
                text: "Second Page"
            }

            MouseArea {
                id: mouseArea
                anchors.fill: parent
                onClicked: mtext.text += " Clicked"
            }
        }
    }

    Row {
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        Button {
            id: next
            text: "Next"
            onClicked: {
                stackView.push({ item: stackView.items[1], destroyOnPop:false })
                enabled = false
                prev.enabled = true
            }
        }

        Button {
            id: prev
            text: "Prev"
            enabled: false
            onClicked: {
                stackView.pop()
                enabled = false
                next.enabled = true
            }
        }
    }
}
通过在源代码中直接定义
rectage
对象,可以获得相同的正确结果。在这种情况下,由于
destroonpop
设置为false,我们有两个
被重新出租到
窗口

Rectangle {
    id: comp1
    color: "lightgray"
    visible: false

    Text {
        id: mtext
        anchors.centerIn: parent
        text: "First Page"
    }
}

Rectangle {
   id: comp2
   color: "lightgreen"
   visible: false

   Text {
        id: mtext2
        anchors.centerIn: parent
        text: "Second Page"
   }

   MouseArea {
       id: mouseArea
       anchors.fill: parent
       onClicked: mtext2.text += " Clicked"
   }
}

文档引用的是
对象,而您使用的是
组件
s。组件是而不是
,因为它不是从
派生出来的,这一点非常清楚。实际上,
组件
的行为类似于实例化到特定对象的

每次你打电话

stackView.push( { item: comp2, destroyOnPop:false } )
生成组件
comp2
的新“实例”,并使用该新实例代替前一个实例。我强烈建议阅读以更好地理解
组件
对象和生成的实例之间的相关性

实例可以通过
createObject
函数生成。因此,您可以创建
s的数组,并使用这样的
s来代替
组件
s。最终代码如下所示:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2

Window {
    visible: true
    width: 320
    height: 320

    StackView {
        id: stackView
        anchors.fill: parent
        Component.onCompleted: {
            push( { item: items[0], destroyOnPop:false })
        }
        property variant items: [comp1.createObject(), comp2.createObject()]  // objects from the components
    }

    Component {
        id: comp1
        Rectangle {
            color: "lightgray"

            Text {
                id: mtext
                anchors.centerIn: parent
                text: "First Page"
            }
        }
    }

    Component {
        id: comp2
        Rectangle {
            color: "lightgreen"

            Text {
                id: mtext
                anchors.centerIn: parent
                text: "Second Page"
            }

            MouseArea {
                id: mouseArea
                anchors.fill: parent
                onClicked: mtext.text += " Clicked"
            }
        }
    }

    Row {
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        Button {
            id: next
            text: "Next"
            onClicked: {
                stackView.push({ item: stackView.items[1], destroyOnPop:false })
                enabled = false
                prev.enabled = true
            }
        }

        Button {
            id: prev
            text: "Prev"
            enabled: false
            onClicked: {
                stackView.pop()
                enabled = false
                next.enabled = true
            }
        }
    }
}
通过在源代码中直接定义
rectage
对象,可以获得相同的正确结果。在这种情况下,由于
destroonpop
设置为false,我们有两个
被重新出租到
窗口

Rectangle {
    id: comp1
    color: "lightgray"
    visible: false

    Text {
        id: mtext
        anchors.centerIn: parent
        text: "First Page"
    }
}

Rectangle {
   id: comp2
   color: "lightgreen"
   visible: false

   Text {
        id: mtext2
        anchors.centerIn: parent
        text: "Second Page"
   }

   MouseArea {
       id: mouseArea
       anchors.fill: parent
       onClicked: mtext2.text += " Clicked"
   }
}

文档引用的是
对象,而您使用的是
组件
s。组件是而不是
,因为它不是从
派生出来的,这一点非常清楚。实际上,
组件
的行为类似于实例化到特定对象的

每次你打电话

stackView.push( { item: comp2, destroyOnPop:false } )
生成组件
comp2
的新“实例”,并使用该新实例代替前一个实例。我强烈建议阅读以更好地理解
组件
对象和生成的实例之间的相关性

实例可以通过
createObject
函数生成。因此,您可以创建
s的数组,并使用这样的
s来代替
组件
s。最终代码如下所示:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2

Window {
    visible: true
    width: 320
    height: 320

    StackView {
        id: stackView
        anchors.fill: parent
        Component.onCompleted: {
            push( { item: items[0], destroyOnPop:false })
        }
        property variant items: [comp1.createObject(), comp2.createObject()]  // objects from the components
    }

    Component {
        id: comp1
        Rectangle {
            color: "lightgray"

            Text {
                id: mtext
                anchors.centerIn: parent
                text: "First Page"
            }
        }
    }

    Component {
        id: comp2
        Rectangle {
            color: "lightgreen"

            Text {
                id: mtext
                anchors.centerIn: parent
                text: "Second Page"
            }

            MouseArea {
                id: mouseArea
                anchors.fill: parent
                onClicked: mtext.text += " Clicked"
            }
        }
    }

    Row {
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        Button {
            id: next
            text: "Next"
            onClicked: {
                stackView.push({ item: stackView.items[1], destroyOnPop:false })
                enabled = false
                prev.enabled = true
            }
        }

        Button {
            id: prev
            text: "Prev"
            enabled: false
            onClicked: {
                stackView.pop()
                enabled = false
                next.enabled = true
            }
        }
    }
}
通过在源代码中直接定义
rectage
对象,可以获得相同的正确结果。在这种情况下,由于
destroonpop
设置为false,我们有两个
被重新出租到
窗口

Rectangle {
    id: comp1
    color: "lightgray"
    visible: false

    Text {
        id: mtext
        anchors.centerIn: parent
        text: "First Page"
    }
}

Rectangle {
   id: comp2
   color: "lightgreen"
   visible: false

   Text {
        id: mtext2
        anchors.centerIn: parent
        text: "Second Page"
   }

   MouseArea {
       id: mouseArea
       anchors.fill: parent
       onClicked: mtext2.text += " Clicked"
   }
}

文档引用的是
对象,而您使用的是
组件
s。组件是而不是
,因为它不是从
派生出来的,这一点非常清楚。实际上,
组件
的行为类似于实例化到特定对象的

每次你打电话

stackView.push( { item: comp2, destroyOnPop:false } )
生成组件
comp2
的新“实例”,并使用该新实例代替前一个实例。我强烈建议阅读以更好地理解
组件
对象和生成的实例之间的相关性

实例可以通过
createObject
函数生成。因此,您可以创建
s的数组,并使用这样的
s来代替
组件
s。最终代码如下所示:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2

Window {
    visible: true
    width: 320
    height: 320

    StackView {
        id: stackView
        anchors.fill: parent
        Component.onCompleted: {
            push( { item: items[0], destroyOnPop:false })
        }
        property variant items: [comp1.createObject(), comp2.createObject()]  // objects from the components
    }

    Component {
        id: comp1
        Rectangle {
            color: "lightgray"

            Text {
                id: mtext
                anchors.centerIn: parent
                text: "First Page"
            }
        }
    }

    Component {
        id: comp2
        Rectangle {
            color: "lightgreen"

            Text {
                id: mtext
                anchors.centerIn: parent
                text: "Second Page"
            }

            MouseArea {
                id: mouseArea
                anchors.fill: parent
                onClicked: mtext.text += " Clicked"
            }
        }
    }

    Row {
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        Button {
            id: next
            text: "Next"
            onClicked: {
                stackView.push({ item: stackView.items[1], destroyOnPop:false })
                enabled = false
                prev.enabled = true
            }
        }

        Button {
            id: prev
            text: "Prev"
            enabled: false
            onClicked: {
                stackView.pop()
                enabled = false
                next.enabled = true
            }
        }
    }
}
通过在源代码中直接定义
rectage
对象,可以获得相同的正确结果。在这种情况下,由于
destroonpop
设置为false,我们有两个
被重新出租到
窗口

Rectangle {
    id: comp1
    color: "lightgray"
    visible: false

    Text {
        id: mtext
        anchors.centerIn: parent
        text: "First Page"
    }
}

Rectangle {
   id: comp2
   color: "lightgreen"
   visible: false

   Text {
        id: mtext2
        anchors.centerIn: parent
        text: "Second Page"
   }

   MouseArea {
       id: mouseArea
       anchors.fill: parent
       onClicked: mtext2.text += " Clicked"
   }
}

在第二种情况下,矩形将直接加载,而不是加载到StackView中,这就是我考虑组件的原因。但没有考虑到该组件不是一个项目。现在很清楚:)在第二种情况下,矩形将直接加载,而不是加载到StackView中,这就是我考虑组件的原因。但我没有考虑过这个问题