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

2024-06-30 13:12:14 发布

您现在位置:Python中文网/ 问答频道 /正文

PySide Signal argument can't be retrieved from QML

根据这篇文章,PySide2(版本>;5.12.5)支持带有命名参数的信号,如PyQt5。所以我在PySide2(5.14.2)中进行了尝试,得到了如下错误

file:///E:/QML/projects/main.qml:72:5: Cannot assign to non-existent property "onSpitInput"

告诉我这里怎么了

*app.py

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)
 }

Tags: importselfidapposmaintopsys
1条回答
网友
1楼 · 发布于 2024-06-30 13:12:14

使用以下代码时:

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

您正在声明OnPitInput属于“root”,这显然是错误的,因为它属于“controller”,因此失败

在这种情况下,您应该使用^{}

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_())

相关问题 更多 >