带有命名参数的PySide2(5.14.2)信号


无法从QML 检索PySide Signal参数

根据这篇文章,PySide2(版本>5.12.5(支持像PyQt5中那样带有命名参数的信号。所以我在PySide2(5.14.2(中尝试了一下,得到了类似的错误

file:///E:/QML/projects/main.qml:72:5:无法分配给不存在的属性"onSpitInput">

告诉我这里出了什么问题。

*应用程序

import os
import sys
from PySide2 import QtCore, QtGui, QtWidgets, QtQml
class controller(QtCore.QObject):
spitInput = QtCore.Signal(str, arguments=['userinput'])
def __init__(self):
QtCore.QObject.__init__(self)
@QtCore.Slot(int, result=list)
def getUserInput(self, first):
self.spitInput.emit(str(first) + 'is the value given by user')

controller = controller()
app = QtWidgets.QApplication(sys.argv)
current_dir = os.path.dirname(os.path.realpath(__file__))
engine = QtQml.QQmlApplicationEngine()
engine.addImportPath(current_dir)
engine.rootContext().setContextProperty("controller", controller)
filename = os.path.join(current_dir, "main.qml")
engine.load(QtCore.QUrl.fromLocalFile(filename))
if not engine.rootObjects():
sys.exit(-1)
engine.quit.connect(app.quit)
sys.exit(app.exec_())

main.qml

import QtQuick 2.13
import QtQuick.Controls 2.13
ApplicationWindow {
id: root
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle {
id: bufferRectId
width: 640
height: 480
anchors.fill: parent
TextField{
id:firstTextInputFieldId
font.pointSize: 16
anchors.top: parent.top
anchors.left: parent.left
anchors.topMargin: 10
anchors.horizontalCenter: parent.horizontalCenter
}
Button{
id:calcButtonId
width: 60
height: 30
text: "Click Me"
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.topMargin: 60
onClicked: {
controller.getUserInput(firstTextInputFieldId.text)
}
}
}
onSpitInput: console.log(userinput)
}

当您使用以下代码时:

ApplicationWindow {
id: root
// ...
onSpitInput: console.log(userinput)
}

您声明onSpitInput属于"根",这显然是错误的,因为它属于"控制器",因此失败。

在这种情况下,您应该使用Connections:

import QtQuick 2.13
import QtQuick.Controls 2.13
ApplicationWindow {
id: root
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle {
id: bufferRectId
anchors.fill: parent
TextField{
id:firstTextInputFieldId
font.pointSize: 16
anchors.top: parent.top
anchors.left: parent.left
anchors.topMargin: 10
anchors.horizontalCenter: parent.horizontalCenter
}
Button{
id:calcButtonId
width: 60
height: 30
text: "Click Me"
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.topMargin: 60
onClicked: controller.getUserInput(firstTextInputFieldId.text)
}
}
Connections{
target: controller
onSpitInput: console.log(userinput)
}
}

另一方面,还有另一个错误:getUserInput方法接收一个字符串,不返回任何内容,但根据您的代码,它必须接收一个整数并返回一个列表。正确的是:

import os
import sys
from PySide2 import QtCore, QtGui, QtWidgets, QtQml

class controller(QtCore.QObject):
spitInput = QtCore.Signal(str, arguments=["userinput"])
@QtCore.Slot(str)
def getUserInput(self, first):
self.spitInput.emit("{} is the value given by user".format(first))

if __name__ == "__main__":
controller = controller()
app = QtWidgets.QApplication(sys.argv)
current_dir = os.path.dirname(os.path.realpath(__file__))
engine = QtQml.QQmlApplicationEngine()
engine.addImportPath(current_dir)
engine.rootContext().setContextProperty("controller", controller)
filename = os.path.join(current_dir, "main.qml")
engine.load(QtCore.QUrl.fromLocalFile(filename))
if not engine.rootObjects():
sys.exit(-1)
engine.quit.connect(app.quit)
sys.exit(app.exec_())

最新更新