Skip to content Skip to sidebar Skip to footer

Signal With Named Parameter

I am trying to replicate below example using PySide2. https://evileg.com/en/post/242/ But as PySide2 doesn't support emitting a Signal with named parameter to QML, i have no clue a

Solution 1:

You can not replicate it like this, if you want to implement the project you can make the slot return the value:

main.py

from PySide2 import QtCore, QtGui, QtQml


classCalculator(QtCore.QObject):
    # Slot for summing two numbers    @QtCore.Slot(int, int, result=int)defsum(self, arg1, arg2):
        return arg1 + arg2

    # Slot for subtraction of two numbers    @QtCore.Slot(int, int, result=int)defsub(self, arg1, arg2):
        return arg1 - arg2


if __name__ == "__main__":
    import os
    import sys

    # Create an instance of the application
    app = QtGui.QGuiApplication(sys.argv)
    # Create QML engine
    engine = QtQml.QQmlApplicationEngine()
    # Create a calculator object
    calculator = Calculator()
    # And register it in the context of QML
    engine.rootContext().setContextProperty("calculator", calculator)
    # Load the qml file into the engine
    file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "main.qml")
    engine.load(file)

    engine.quit.connect(app.quit)
    sys.exit(app.exec_())

main.qml

importQtQuick2.5importQtQuick.Controls1.4importQtQuick.Layouts1.2ApplicationWindow {
    visible:truewidth:640height:240title:qsTr("PyQt5loveQML")color:"whitesmoke"GridLayout {
        anchors.top:parent.topanchors.left:parent.leftanchors.right:parent.rightanchors.margins:9columns:4rows:4rowSpacing:10columnSpacing:10Text {
            text:qsTr("Firstnumber")
        }
 
        //InputfieldofthefirstnumberTextField {
            id:firstNumber
        }
 
        Text {
            text:qsTr("Secondnumber")
        }
 
        //InputfieldofthesecondnumberTextField {
            id:secondNumber
        }
 
        Button {
            height:40Layout.fillWidth:truetext:qsTr("Sumnumbers")Layout.columnSpan:2onClicked: {
                //InvokethecalculatorslottosumthenumberssumResult.text=calculator.sum(firstNumber.text, secondNumber.text)
            }
        }
 
        Text {
            text:qsTr("Result")
        }
 
        //HereweseetheresultofsumText {
            id:sumResult
        }
 
        Button {
            height:40Layout.fillWidth:truetext:qsTr("Subtractionnumbers")Layout.columnSpan:2onClicked: {
                //InvokethecalculatorslottosubtractthenumberssubResult.text=calculator.sub(firstNumber.text, secondNumber.text)
            }
        }
 
        Text {
            text:qsTr("Result")
        }
 
        //HereweseetheresultofsubtractionText {
            id:subResult
        }
    }
}

Note: For PyQt5 change Slot to pyqtSlot.

Post a Comment for "Signal With Named Parameter"