栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

具有指定参数的信号

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

具有指定参数的信号

您不能像这样复制它,如果要实现项目,则可以使插槽返回值:

main.py

from PySide2 import QtCore, QtGui, QtQmlclass 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 - arg2if __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.5import QtQuick.Controls 1.4import QtQuick.Layouts 1.2ApplicationWindow {    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        }    }}

注意:将 PyQt5更改

Slot
pyqtSlot



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/653526.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号