Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Passing arguments from PySide2 to QML.
Qt 6.11 is out! See what's new in the release blog

Passing arguments from PySide2 to QML.

Scheduled Pinned Locked Moved Unsolved Language Bindings
2 Posts 2 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pleaseHelpSteve
    wrote on last edited by
    #1

    Hi all,
    I'm current trying to figure out how to use Python with QT QML. So far I have been successful in passing info from QML to python with no problems, however after much searching and frustration I have not been able to go from Python to QML.

    So I'm currently using
    Python code:
    valfeedback = Signal(float, name = "pos")
    self.valfeedback.emit(self.m_currentValue)
    QML
    Connections {
    target: Manager

        onValfeedback: {
    
            label.text = Manager.pos
        }
    }
    

    }

    But this just complains about Error: Cannot assign [undefined] to QString But I'm not sure how it is supposed to be defined???

    I'm sure that I'm missing something really simple but have spent hours on this with no results - Please help........

    My sample code below..

    import sys
    import os

    from PySide2.QtCore import Qt, QObject, Signal, Slot, Property
    from PySide2.QtGui import QGuiApplication
    from PySide2.QtQml import QQmlApplicationEngine

    class Manager(QObject):

    def __init__(self):
        QObject.__init__(self)
        self.m_currentValue = 3.4
        self.m_buttonValue = 0
        self.labelInfo = "47"
    
    valfeedback = Signal(float, name = "pos")
    
    @Property(float)
    def currentValue(self):
       return self.m_currentValue
    @currentValue.setter
    def setCurrentValue(self, val):
        if self.m_currentValue == val:
            return
        self.m_currentValue = val
        print(self.m_currentValue)
        self.valfeedback.emit(self.m_currentValue)
    
    
    @Property(int)
    def buttonValue(self):
        return self.m_buttonValue
    @buttonValue.setter
    def setButtonValue(self, val):
        self.m_buttonValue = val
        print(val)
    

    if name == "main":
    os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    manager = Manager()
    ctx = engine.rootContext()
    ctx.setContextProperty("Manager", manager)
    engine.load('view4.qml')
    if not engine.rootObjects():
    sys.exit(-1)

    sys.exit(app.exec_())
    

    import QtQuick 2.10
    import QtQuick.Controls 2.3
    import QtQuick.Layouts 1.0

    ApplicationWindow {
    id: applicationWindow

    title: qsTr("Test Invoke")
    visible: true
    
    width: 600
    height: 500
    
    Slider {
        id: slider
        x: 78
        y: 330
        width: 444
        height: 40
        value: 0.5
        property bool updateValueWhileDragging: false
        onMoved: Manager.currentValue = value
    }
    
    Button {
        id: button
        x: 250
        y: 230
        text: qsTr("Press Me Please")
        checkable: true
        onPressed: Manager.buttonValue = 1
    }
    
    
    Label {
        id: label
        x: 37
        y: 65
        horizontalAlignment: Text.AlignHCenter
        font.pointSize: 35
    }
    
    Connections {
        target: Manager
    
        onValfeedback: {
    
            label.text = Manager.pos
        }
    }
    

    }

    D 1 Reply Last reply
    0
    • P pleaseHelpSteve

      Hi all,
      I'm current trying to figure out how to use Python with QT QML. So far I have been successful in passing info from QML to python with no problems, however after much searching and frustration I have not been able to go from Python to QML.

      So I'm currently using
      Python code:
      valfeedback = Signal(float, name = "pos")
      self.valfeedback.emit(self.m_currentValue)
      QML
      Connections {
      target: Manager

          onValfeedback: {
      
              label.text = Manager.pos
          }
      }
      

      }

      But this just complains about Error: Cannot assign [undefined] to QString But I'm not sure how it is supposed to be defined???

      I'm sure that I'm missing something really simple but have spent hours on this with no results - Please help........

      My sample code below..

      import sys
      import os

      from PySide2.QtCore import Qt, QObject, Signal, Slot, Property
      from PySide2.QtGui import QGuiApplication
      from PySide2.QtQml import QQmlApplicationEngine

      class Manager(QObject):

      def __init__(self):
          QObject.__init__(self)
          self.m_currentValue = 3.4
          self.m_buttonValue = 0
          self.labelInfo = "47"
      
      valfeedback = Signal(float, name = "pos")
      
      @Property(float)
      def currentValue(self):
         return self.m_currentValue
      @currentValue.setter
      def setCurrentValue(self, val):
          if self.m_currentValue == val:
              return
          self.m_currentValue = val
          print(self.m_currentValue)
          self.valfeedback.emit(self.m_currentValue)
      
      
      @Property(int)
      def buttonValue(self):
          return self.m_buttonValue
      @buttonValue.setter
      def setButtonValue(self, val):
          self.m_buttonValue = val
          print(val)
      

      if name == "main":
      os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
      app = QGuiApplication(sys.argv)
      engine = QQmlApplicationEngine()
      manager = Manager()
      ctx = engine.rootContext()
      ctx.setContextProperty("Manager", manager)
      engine.load('view4.qml')
      if not engine.rootObjects():
      sys.exit(-1)

      sys.exit(app.exec_())
      

      import QtQuick 2.10
      import QtQuick.Controls 2.3
      import QtQuick.Layouts 1.0

      ApplicationWindow {
      id: applicationWindow

      title: qsTr("Test Invoke")
      visible: true
      
      width: 600
      height: 500
      
      Slider {
          id: slider
          x: 78
          y: 330
          width: 444
          height: 40
          value: 0.5
          property bool updateValueWhileDragging: false
          onMoved: Manager.currentValue = value
      }
      
      Button {
          id: button
          x: 250
          y: 230
          text: qsTr("Press Me Please")
          checkable: true
          onPressed: Manager.buttonValue = 1
      }
      
      
      Label {
          id: label
          x: 37
          y: 65
          horizontalAlignment: Text.AlignHCenter
          font.pointSize: 35
      }
      
      Connections {
          target: Manager
      
          onValfeedback: {
      
              label.text = Manager.pos
          }
      }
      

      }

      D Offline
      D Offline
      dadara
      wrote on last edited by
      #2

      @pleaseHelpSteve: What worked for me is using QStringModel as described here https://stackoverflow.com/questions/50609986/how-to-connect-python-and-qml-with-pyside2 (or here http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html)

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved