PySide信号参数无法从QML中检索

6
我注意到QML可以使用Connections对象接收Python发出的信号。不幸的是,我无法弄清楚如何让该对象接收该信号的参数。
我创建了一个最小化的测试用例来演示我的意图:
min.py
from PySide import QtCore, QtGui, QtDeclarative
import sys

# init Qt
app = QtGui.QApplication(sys.argv)

# set up the signal
class Signaller(QtCore.QObject):
    emitted = QtCore.Signal(str)

signaller = Signaller()

# Load the QML
qt_view = QtDeclarative.QDeclarativeView()
context = qt_view.rootContext()
context.setContextProperty('signaller', signaller)
qt_view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)
qt_view.setSource('min.qml')
qt_view.show()

# launch the signal
signaller.emitted.emit("Please display THIS text!")

# Run!
app.exec_()

并且 min.qml

import QtQuick 1.0

Rectangle {
    width:300; height:100

    Text {
        id: display
        text: "No signal yet detected!"

        Connections {
            target: signaller
            onEmitted: {
                display.text = "???" //how to get the argument?
            }
        }
    }
}
3个回答

6
截至Qt for Python版本5.12.5、5.13.1,它的工作方式与PyQt相同:
from PySide2.QtCore import Signal

sumResult = Signal(int, arguments=['sum'])
sumResult.emit(42)

QML:

onSumResult: console.log(sum)

在 PySide2 (5.14.2) 中无法工作,出现错误“无法分配给不存在的属性”。 - Ruchit
在 PySide2 5.15.0 上对我来说完全正常运行。 - alexm

5
截至Qt 4.8版本,PySide完全不处理信号参数名称。
但是,您可以使用命名参数创建一个QML信号,并使用Javascript将您的python信号连接到该信号:
import QtQuick 1.0

Rectangle {
    width:300; height:100


    Text {
        id: display
        text: "No signal yet detected!"

        signal reemitted(string text)
        Component.onCompleted: signaller.emitted.connect(reemitted)

        onReemitted: {
          display.text = text;        
        }
    }
}

0

抱歉,我无法发表评论,因为我需要更高的声望。 对于“无法分配给不存在的属性”的回应,它是由您初始化应用程序的顺序引起的。 您的根上下文对象需要在引擎之前创建。

正确:

context = Context() engine = QQmlApplicationEngine() engine.rootContext().setContextProperty("context", context)

不正确:

engine = QQmlApplicationEngine() context = Context() engine.rootContext().setContextProperty("context", context)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接