我是python和qml编程的初学者,我正在做一个项目 我的项目需要有很多UI表单我正在使用qml来创建这些UI
假设我们有表格A,B,C,当申请加载我需要它打开表格A和表格A包含按钮,我点击并打开表格B和表格A关闭,表格B它有按钮,我点击它打开表格C,表格B关闭...... Plis帮助我完成这些项目
main.py
import sys
from PyQt5.QtCore import QObject, QUrl, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
ctx = engine.rootContext()
ctx.setContextProperty("main", engine)
ctx2 = engine.rootContext()
ctx2.setContextProperty("main", engine)
engine.load('form1.qml')
win = engine.rootObjects()[0]
def pageC():
engine.load('form3.qml')
win2 = engine.rootObjects()[0]
button1 = win2.findChild(QObject, "form3")
button1.clicked.connect(pageC)
win2.show()
def newPage():
engine.load('form2.qml')
win = engine.rootObjects()[0]
win.show()
button1=win.findChild(QObject, "form2")
button1.clicked.connect(newPage)
win.show()
sys.exit(app.exec_())
form1.qml
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.5
Window {
visible: true
width: 200
height: 200
title: qsTr("Hello World")
maximumHeight: 200
minimumHeight: 200
maximumWidth: 200
minimumWidth: 200
Button {
id: button1
objectName: "form2"
x: 22
y: 71
width: 157
height: 59
text: qsTr("Page a")
onClicked: callPageB()
}
}
form2.qml
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.5
Window {
visible: true
width: 200
height: 200
title: qsTr("Hello World")
maximumHeight: 200
minimumHeight: 200
maximumWidth: 200
minimumWidth: 200
Button {
id: button1
objectName: "form3"
x: 22
y: 71
width: 157
height: 59
text: qsTr("Page B")
onClicked: callPageC()
}
}
form3.qml
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.5
Window {
visible: true
width: 200
height: 200
title: qsTr("Hello World")
maximumHeight: 200
minimumHeight: 200
maximumWidth: 200
minimumWidth: 200
Button {
id: button1
x: 22
y: 71
width: 157
height: 59
text: qsTr("Page C")
}
}
答案 0 :(得分:0)
将qmls添加到QQmlApplicationEngine
时我们可以通过rootObjects()
,所以为了做到这一点,我只创建了以下类,但是有一些限制,Window中的每个按钮都应该有{{1作为文件的名称,必须是Window的子项:
<强> example.qml:强>
objectName
在下一部分中,这个类名为你的qmls。
Window {
...
Button {
objectName: "example"
}
...
}