从QML插槽调用Python代码时,无法为QString错误分配[undefined]

时间:2019-07-22 19:53:14

标签: python qt qml pyside2

我在QML中定义了以下插槽:

onTextResourceFileToReadChanged: {
        // textArea.text = "Hello world!" // Works, sets text as expected
        textArea.text = TextResourceReader.hello_world() // Doesn't work: "Error: Cannot assign [undefined] to QString"
    }

我在Python中定义了以下插槽:

class Foo(QtCore.QObject):
    def __init__(self, parent=None):
        super().__init__(parent)

@QtCore.Slot()
    def hello_world(self):
        return "Hello world!"

我的Python类已注册:

engine.rootContext().setContextProperty("Foo", foo)

但是,当调用我的QML插槽onTextResourceFileToReadChanged时,出现以下错误:

Error: Cannot assign [undefined] to QString

0 个答案:

没有答案