PySide Signal argument can't be retrieved from

2019-02-25 15:16发布

I've noticed that QML can receive a signal emitted from Python by using the Connections object. Unfortunately, I can't figure out how to get that object to receive the arguments of that signal.

I've created a minimal test case that demonstrates what I want to do:

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

And 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?
            }
        }
    }
}

Thank you!

标签: qt qml pyside
1条回答
Lonely孤独者°
2楼-- · 2019-02-25 15:48

As of Qt 4.8, PySide doesn't handle signal parameter names at all.

But you can create a QML signal with named parameters and connect your python signal to it using Javascript:

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;        
        }
    }
}
查看更多
登录 后发表回答