无法从QML中检索PySide Signal参数

发布于 2021-01-29 17:24:39

我注意到QML可以通过使用Connections对象来接收Python发出的信号。不幸的是,我无法弄清楚如何使该对象接收该信号的参数。

我创建了一个最小的测试用例,演示了我想做的事情:

最低

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

和最小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?
            }
        }
    }
}
关注者
0
被浏览
140
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    从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;        
            }
        }
    }
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看