pyqt - Multiple Page on Python and QML -
i’m beginner on programming in python , qml , i'm doing project project need have many ui forms i’m using qml create these ui
lets have form a,b,c , when application load need open form , form contain button click , open form b , form close, form b have button click open form c , form b close… plis me on these project
main.py
import sys pyqt5.qtcore import qobject, qurl, qt pyqt5.qtwidgets import qapplication 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") } }
when adding qmls qqmlapplicationengine
can through rootobjects()
, created following class, there restrictions, each button inside window should have objectname
name of file , must children of window:
example.qml:
window { ... button { objectname: "example" } ... }
in next part class name of qmls.
class engine(qqmlapplicationengine): counter = 0 def __init__(self, qmls, parent=none): qqmlapplicationengine.__init__(self, parent) [self.load("{}.qml".format(qml)) qml in qmls] i, root in enumerate(self.rootobjects()): if root != self.rootobjects()[0]: root.close() if root != self.rootobjects()[-1]: button= root.findchild(qobject, qmls[i]) button.clicked.connect(self.closeandopen) def closeandopen(self): self.rootobjects()[self.counter].close() self.counter += 1 self.rootobjects()[self.counter].show() if __name__ == "__main__": app = qapplication(sys.argv) engine = engine(['form1', 'form2', 'form3']) sys.exit(app.exec_())
wiki
Comments
Post a Comment