Passing arguments from PySide2 to QML.



  • 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
        }
    }
    

    }




Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.