带有命名参数的信号
Posted
技术标签:
【中文标题】带有命名参数的信号【英文标题】:Signal with named parameter 【发布时间】:2018-07-29 15:36:09 【问题描述】:我正在尝试使用 PySide2 复制以下示例。
https://evileg.com/en/post/242/
但由于 PySide2 不支持向 QML 发出,我不知道如何使用 PySide2 来做到这一点?
这是我的代码
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_())
【问题讨论】:
【参考方案1】:你不能这样复制,如果你想实现项目你可以让槽返回值:
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
注意:对于 PyQt5,将 Slot
更改为 pyqtSlot
。
【讨论】:
出现以下错误。任何的想法 ?什么坏了。 QQmlApplicationEngine 无法加载组件文件://E:/code/QML/calc.qml:1 无法为模块“QtQuick”加载插件:无法加载库 C:\Python27\lib\site-packages\PySide2\qml\ QtQuick.2\qtquick2plugin.dll: 找不到指定的程序。 @Ruchit .dll 似乎与您的操作系统不兼容,您是如何安装 pyside2 的? 将C:\Python27\Lib\site-packages\PySide2
添加到PATH 变量后工作正常。
如果你想一遍又一遍地从 python 调用函数中更改 progressBar 值以获得结果是非常糟糕的方法。我会对如何使用 PySide2 中的信号来做这件事很感兴趣以上是关于带有命名参数的信号的主要内容,如果未能解决你的问题,请参考以下文章
使用 NHibernate 作为带有输入参数的命名查询调用存储过程
Laravel:如何通过javascript加载带有动态参数的命名路由