【问题标题】:Access QML Stackview from a file different than the one that contains it从不同于包含它的文件访问 QML Stackview
【发布时间】:2019-02-08 04:43:22
【问题描述】:

我正在制作一个 QML 应用程序。我已经定义了一个 main.qml,我在其中添加了一个 StackView。我已经在这个“主”堆栈视图上加载了一个页面,但来自 main.qml。现在我需要从另一个文件(更具体地说,从我之前在此 stackview 上推送的页面之一)向这个 stackview 推送一些东西。

这是我做的。

main.qml

ApplicationWindow {
    id: window
    visible: true
    width: 320
    height: 568
    title: qsTr("Stack")

    header: ToolBar {
        contentHeight: toolButton.implicitHeight

        ToolButton {
            id: toolButton
            text: stackView.depth > 1 ? "\u25C0" : ""
            font.pixelSize: Qt.application.font.pixelSize * 1.6
            onClicked: {
                if ( stackView.depth > 1 ) {
                    stackView.pop( )
                } else {
                    ...
                }


            }
        }

        Label {
            text: stackView.currentItem.title
            anchors.centerIn: parent
        }
    }


    StackView {
        id: stackView
        initialItem: "ListaInventarios.qml"
        anchors.fill: parent
    }


}

ListaInventarios.qml

Page {
    width: parent.width
    height: parent.height
    id: listaInventarios
    title: "Inventarios"


    ListView {
        id: lvInventarios
        anchors.fill: parent
        model: ListModel { ... }
        delegate: Component {
            Item {
                width: parent.width
                height: 40
                Column {
                    Text { text: nombreInventario; font.bold: true }
                    Text { text: fuenteDatos }
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: { lvInventarios.currentIndex = index }
                    onDoubleClicked: {

                    // HERE I NEED TO PUSH A NEW PAGE ON "stackView" (from main.qml)

                    }
                }
            }
        }

        highlight: Rectangle {
            color: "lightblue"
            radius: 5
        }

        focus: true

    }


    footer: ToolBar {
        id: barraInferior
        contentHeight: toolButton.implicitHeight
        Row {
            anchors {
                horizontalCenter: parent.horizontalCenter
                verticalCenter: parent.verticalCenter

            }

            ToolButton {
                text: "Nuevo"
                width: window.width / 2
                onClicked: {
                   ...
                }
            }

            ToolButton {
                text: "Borrar"
                width: window.width / 2
                onClicked: {
                    ...
                }

            }

        }
    }

}

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    使用attached StackView API

    onDoubleClicked: {
        listaInventarios.StackView.view.push("Foo.qml")
    }
    

    请注意,您需要在调用前加上由StackView 管理的项目:listaInventarios

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-29
      • 2014-04-05
      • 2016-12-20
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      相关资源
      最近更新 更多