带有命名参数的信号 [英] Signal with named parameter

查看:65
本文介绍了带有命名参数的信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PySide2 复制以下示例.

https://evileg.com/en/post/242/>

但是由于 PySide2 不支持向 QML 发出带有命名参数的信号,我不知道如何使用 PySide2 执行此操作?

这是我的代码

ma​​in.py

 from PySide2.QtGui import QGuiApplication从 PySide2.QtQml 导入 QQmlApplicationEngine从 PySide2.QtCore 导入 QObject、信号、插槽、属性类计算器(QObject):def __init__(self):QObject.__init__(self)sumResult = 信号(整数)subResult = 信号(整数)@Slot(int, int)def sum(self, arg1, arg2):self.sumResult.emit(arg1 + arg2)@Slot(int, int)def sub(self, arg1, arg2):self.subResult.emit(arg1 - arg2)如果 __name__ == "__main__":导入系统app = QGuiApplication(sys.argv)引擎 = QQmlApplicationEngine()计算器 = 计算器()engine.rootContext().setContextProperty("calculator", 计算器)engine.load("/code/QML/calc.qml")engine.quit.connect(app.quit)sys.exit(app.exec_())

解决方案

不能这样复制,如果要实现项目可以让槽返回值:

ma​​in.py

from PySide2 import QtCore, QtGui, QtQml类计算器(QtCore.QObject):# 两个数字相加的槽@QtCore.Slot(int, int, result=int)def sum(self, arg1, arg2):返回 arg1 + arg2# 两个数相减的槽@QtCore.Slot(int, int, result=int)def sub(self, arg1, arg2):返回 arg1 - arg2如果 __name__ == __main__":导入操作系统导入系统# 创建应用实例app = QtGui.QGuiApplication(sys.argv)# 创建 QML 引擎引擎 = QtQml.QQmlApplicationEngine()# 创建一个计算器对象计算器 = 计算器()# 并在QML的上下文中注册engine.rootContext().setContextProperty(计算器",计算器)# 将qml文件加载到引擎中file = os.path.join(os.path.dirname(os.path.realpath(__file__)), main.qml")引擎加载(文件)engine.quit.connect(app.quit)sys.exit(app.exec_())

ma​​in.qml

导入QtQuick 2.5导入 QtQuick.Controls 1.4导入 QtQuick.Layouts 1.2应用程序窗口{可见:真实宽度:640高度:240标题:qsTr(PyQt5 爱 QML")颜色:whitesmoke"网格布局 {anchors.top: parent.topanchors.left: parent.leftanchors.right: parent.right锚点.边距:9列:4行数:4行间距:10列间距:10文本 {文本:qsTr(第一个数字")}//第一个数字的输入字段文本域 {id:第一个号码}文本 {文本:qsTr(第二个数字")}//第二个数字的输入字段文本域 {id:第二个号码}按钮 {高度:40Layout.fillWidth: 真文本:qsTr(求和数")Layout.columnSpan:2已点击:{//调用计算器槽对数字求和sumResult.text =calculator.sum(firstNumber.text, secondNumber.text)}}文本 {文本:qsTr(结果")}//这里我们看到 sum 的结果文本 {id: sumResult}按钮 {高度:40Layout.fillWidth: 真文本:qsTr(减数")Layout.columnSpan:2已点击:{//调用计算器槽来减去数字subResult.text =calculator.sub(firstNumber.text, secondNumber.text)}}文本 {文本:qsTr(结果")}//这里我们看到减法的结果文本 {id:子结果}}}

注意:对于 PyQt5,将 Slot 更改为 pyqtSlot.

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 about how to do this using PySide2 ?

Here is my code

main.py

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


class Calculator(QObject):
    def __init__(self):
        QObject.__init__(self)

    sumResult = Signal(int)
    subResult = Signal(int)

    @Slot(int, int)
    def sum(self, arg1, arg2):
        self.sumResult.emit(arg1 + arg2)

    @Slot(int, int)
    def sub(self, arg1, arg2):
        self.subResult.emit(arg1 - arg2)


if __name__ == "__main__":
    import sys

    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    calculator = Calculator()
    engine.rootContext().setContextProperty("calculator", calculator)
    engine.load("/code/QML/calc.qml")

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

解决方案

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


class Calculator(QtCore.QObject):
    # Slot for summing two numbers
    @QtCore.Slot(int, int, result=int)
    def sum(self, arg1, arg2):
        return arg1 + arg2

    # Slot for subtraction of two numbers
    @QtCore.Slot(int, int, result=int)
    def sub(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

import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.2
 
ApplicationWindow {
    visible: true
    width: 640
    height: 240
    title: qsTr("PyQt5 love QML")
    color: "whitesmoke"
 
    GridLayout {
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.margins: 9
 
        columns: 4
        rows: 4
        rowSpacing: 10
        columnSpacing: 10
 
        Text {
            text: qsTr("First number")
        }
 
        // Input field of the first number
        TextField {
            id: firstNumber
        }
 
        Text {
            text: qsTr("Second number")
        }
 
        // Input field of the second number
        TextField {
            id: secondNumber
        }
 
        Button {
            height: 40
            Layout.fillWidth: true
            text: qsTr("Sum numbers")
 
            Layout.columnSpan: 2
 
            onClicked: {
                // Invoke the calculator slot to sum the numbers
                sumResult.text = calculator.sum(firstNumber.text, secondNumber.text)
            }
        }
 
        Text {
            text: qsTr("Result")
        }
 
        // Here we see the result of sum
        Text {
            id: sumResult
        }
 
        Button {
            height: 40
            Layout.fillWidth: true
            text: qsTr("Subtraction numbers")
 
            Layout.columnSpan: 2
 
            onClicked: {
                // Invoke the calculator slot to subtract the numbers
                subResult.text = calculator.sub(firstNumber.text, secondNumber.text)
            }
        }
 
        Text {
            text: qsTr("Result")
        }
 
        // Here we see the result of subtraction
        Text {
            id: subResult
        }
    }
}

Note: For PyQt5 change Slot to pyqtSlot.

这篇关于带有命名参数的信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆