IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PyQt Python Discussion :

PyQt - modifier la valeur d'un widget QML


Sujet :

PyQt Python

  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Juillet 2005
    Messages
    491
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 491
    Par défaut PyQt - modifier la valeur d'un widget QML
    Bonjour,

    J'ai un widget écrit en qml, et je voudrais modifier la valeur via python.

    mon qml :
    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
            }
        }
     
    }
    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
    # -*- 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_())
    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
    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)
    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
    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 ()
    Mais mon widget ne se met pas à jour...
    Qu'est ce que j'ai oublié? Est-ce la bonne méthode que j'utilise ?

    Merci,
    Nico

  2. #2
    Expert confirmé

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 307
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 307
    Par défaut
    Salut,

    Ici:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
            gauge=self.findChild(QObject,'quickWidget')
    si je suis la logique de tes instances, ça devrait plutôt être:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
            gauge=self.ui.findChild(QObject,'quickWidget')

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Juillet 2005
    Messages
    491
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 491
    Par défaut
    Si je met ça, ma fenêtre ne s'ouvre pas, et j'ai un message d'erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    AttributeError: 'Ui_MainWindow' object has no attribute 'findChild'

  4. #4
    Expert confirmé

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 307
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 307
    Par défaut
    Je t'avoue que je n'ai jamais utilisé Qml dont l'utilité à partir de PyQt me semble bien mystérieuse.

    Mais puisque tu sais que l'objet s'appelle quickWidget alors simplement self.ui.quickWidget.setProperty('gauge_value',65)

  5. #5
    Membre éclairé
    Profil pro
    Inscrit en
    Juillet 2005
    Messages
    491
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 491
    Par défaut
    Ca ne fait rien, la valeur reste à 0.

  6. #6
    Expert confirmé

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 307
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 307
    Par défaut
    Au moins tu n'as pas d'erreur, donc l'objet est bon, par contre la valeur ne doit pas être zéro mais 45 selon ta définition du Qml.

    Essaye avec setValue(65), pour ce genre de choses, dont on trouve peu d'exemple (parce que personne ne s'en sert), il faut procéder par tâtonnement.

Discussions similaires

  1. [XSL] Comment modifier la valeur d'une variable?
    Par sorcer1 dans le forum XSL/XSLT/XPATH
    Réponses: 8
    Dernier message: 17/02/2010, 13h26
  2. Réponses: 3
    Dernier message: 04/01/2006, 20h53
  3. Modifier la valeur d'un champ
    Par MJEFF dans le forum Access
    Réponses: 10
    Dernier message: 06/10/2005, 10h59
  4. Modifier la valeur d'un champ en fonction d'une autre...
    Par venividivici dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 25/08/2005, 14h28
  5. [C#] Modifier une valeur dans une DataTable
    Par Scorff dans le forum ASP.NET
    Réponses: 2
    Dernier message: 23/05/2005, 10h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo