从Qt(main.cpp)QML中查看
问题描述:
我在QML中更改视图/文件时遇到问题。在qrc文件中,我有main.qml和second.qml。在main.cpp中我的代码开始我的应用程序:从Qt(main.cpp)QML中查看
QQuickView view;
view.setSource(QUrl(("qrc:///main.qml")));
view.show();
在main.qml是按钮,应该改变以second.qml,但我不知道用什么方式做到这一点。我读过关于qml的内容,但是在任何地方我找到了这些示例
的main.qml:
Item {
id: screen; width: 320; height: 480;
signal exitApp()
signal qmlSignal(string addressIP, int portTCP)
Rectangle {
id: background
anchors.fill: parent; color: "#ffffff";
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 0
anchors.topMargin: 0
Button {
id: loginBtn
text: qsTr("RUN")
anchors.right: parent.right
anchors.rightMargin: 100
anchors.left: parent.left
anchors.leftMargin: 100
anchors.bottom: parent.bottom
anchors.bottomMargin: 170
anchors.top: tcpRow.bottom
anchors.topMargin: 10
onClicked: qmlSignal(addressIPTextField.text, parseInt(portTCPTextField.text))
}
}
Row {
id: tcpRow
x: 8
width: 309
height: 100
anchors.top: ipRow.bottom
anchors.topMargin: 10
anchors.horizontalCenter: parent.horizontalCenter
Label {
id: portTCPLabel
height: 20
text: qsTr("Port TCP")
anchors.left: parent.left
anchors.leftMargin: 10
anchors.right: portTCPTextField.left
anchors.rightMargin: 10
anchors.verticalCenter: parent.verticalCenter
}
}}
答
您可以使用多个StackView
“屏幕” 之间进行导航。要使现有代码适用于StackView
,将每个屏幕移动到其自己的QML文件可能会更容易。例如,移动background
项目进入LoginScreen.qml
:
Rectangle {
id: background
// ...
Button {
onClicked: {
qmlSignal(addressIPTextField.text, parseInt(portTCPTextField.text));
StackView.view.push("qrc:/second.qml");
}
}
}
在这里,我们使用的StackView
附加view
属性来获取访问视图,然后按下第二个屏幕到它。
然后,在main.qml
:
Window {
width: // ...
height: // ...
StackView {
initialItem: LoginScreen {}
}
}
感谢您的回复,但什么意思'StackView.view'?它是从main.cpp中查看的? – Szymson
正如我在我的回答中提到的那样,它是'StackView'的附加属性。请阅读我链接到的文档。 – Mitch