QMetaObject::invokeMethod doesn't find the method

青春壹個敷衍的年華 提交于 2020-01-24 23:10:35

问题


I want to use QMetaObject::invokeMethod to call a method of an object (later it will run in another thread and then invokeMethod comes in handy). I use the Qt 4.8 bindings of PySide 1.2.1 on Python 3.3. The full example is:

from PySide import QtCore

class Tester(QtCore.QObject):
    def __init__(self):
        super().__init__()

    def beep(self):
        print('beep')

if __name__ == '__main__':
    t = Tester()
    QtCore.QMetaObject.invokeMethod(t, 'beep', QtCore.Qt.AutoConnection)

And the output is:

QMetaObject::invokeMethod: No such method Tester::beep()

while I expected beep. The method was not invoked.

So what's wrong? It seems so simple that I cannot find the error.


edit: I got it to work using the `@QtCore.Slot' decoration on the method. Thanks to the comment and the answer.


回答1:


You cannot invoke regular methods, only signals and slots. That is why it is not working for you. See the QMetaObject documentation for details about it:

Invokes the member (a signal or a slot name) on the object obj. Returns true if the member could be invoked. Returns false if there is no such member or the parameters did not match.

Try this decorator:

...
@QtCore.Slot()
def beep(self):
    print('beep')
...

See the following documentation for details as well as this one:

Using QtCore.Slot()

Slots are assigned and overloaded using the decorator QtCore.Slot(). Again, to define a signature just pass the types like the QtCore.Signal() class. Unlike the Signal() class, to overload a function, you don’t pass every variation as tuple or list. Instead, you have to define a new decorator for every different signature. The examples section below will make it clearer.

Another difference is about its keywords. Slot() accepts a name and a result. The result keyword defines the type that will be returned and can be a C or Python type. name behaves the same way as in Signal(). If nothing is passed as name then the new slot will have the same name as the function that is being decorated.



来源:https://stackoverflow.com/questions/23127844/qmetaobjectinvokemethod-doesnt-find-the-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!