Bonjour,
J'ai un widget écrit en qml, et je voudrais modifier la valeur via python.
mon qml :
mon interface (générée par Qt designer, et ensuite je compile le .ui en .py) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 import QtQuick 2.0 import QtQuick.Controls.Styles 1.4 import QtQuick.Extras 1.4 Rectangle { width: 400 height: 400 CircularGauge { property real gauge_value: 45 id: gauge anchors.fill: parent value: gauge_value style: CircularGaugeStyle { labelInset: outerRadius * 0.2 tickmarkLabel: null tickmark: Text { text: styleData.value Text { anchors.horizontalCenter: parent.horizontalCenter anchors.top: parent.bottom text: styleData.index color: "blue" } } minorTickmark: Text { text: styleData.value font.pixelSize: 8 Text { anchors.horizontalCenter: parent.horizontalCenter anchors.top: parent.bottom text: styleData.index font.pixelSize: 8 color: "blue" } } } Text { id: indexText text: "Major and minor indices" anchors.horizontalCenter: parent.horizontalCenter anchors.bottom: valueText.top color: "blue" } Text { id: valueText text: "Major and minor values" anchors.horizontalCenter: parent.horizontalCenter anchors.bottom: parent.bottom } } }
Ensuite, je crée un fichier python pour gérer les événements :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'ROVPilot.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(888, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.quickWidget = QtQuickWidgets.QQuickWidget(self.centralwidget) self.quickWidget.setGeometry(QtCore.QRect(80, 70, 501, 471)) self.quickWidget.setResizeMode(QtQuickWidgets.QQuickWidget.SizeRootObjectToView) self.quickWidget.setSource(QtCore.QUrl("gauge.qml")) self.quickWidget.setObjectName("quickWidget") self.quickWidget_2 = QtQuickWidgets.QQuickWidget(self.centralwidget) self.quickWidget_2.setGeometry(QtCore.QRect(620, 80, 101, 371)) self.quickWidget_2.setResizeMode(QtQuickWidgets.QQuickWidget.SizeRootObjectToView) self.quickWidget_2.setSource(QtCore.QUrl("gauge2.qml")) self.quickWidget_2.setObjectName("quickWidget_2") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 888, 26)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) from PyQt5 import QtQuickWidgets if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
Et le script principal que je lance :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 from PyQt5 import QtCore, QtGui, QtWidgets from ROVPilot import Ui_MainWindow #note the capitalization from PyQt5.QtCore import QObject class MainWindow (QtWidgets.QMainWindow): def __init__ (self, parent = None): super (MainWindow, self).__init__ () self.ui = Ui_MainWindow () self.ui.setupUi (self) #------------do your custom stuff from here on----------- gauge=self.findChild(QObject,'quickWidget') gauge.setProperty('gauge_value',65)
Mais mon widget ne se met pas à jour...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import sys #from PyQt4 import QtGui from PyQt5 import QtCore, QtGui, QtWidgets #works for pyqt5 from mainWindow import MainWindow def main(): app = QtWidgets.QApplication (sys.argv) #works for pyqt5 #app = QtGui.QApplication (sys.argv) #works for pyqt4 m = MainWindow () m.show () sys.exit (app.exec_ () ) if __name__ == '__main__': main ()
Qu'est ce que j'ai oublié? Est-ce la bonne méthode que j'utilise ?
Merci,
Nico
Partager