QMetaObject :: invokeMethod cannot find method

I want to use QMetaObject :: invokeMethod to call an object method (it will later be run in another thread, and then the invokeMethod method will be called). I am using Qt 4.8 PySide 1.2.1 bindings on Python 3.3. Full example:

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 result:

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

while I was expecting a beep . This method has not been called.

So what happened? It seems so simple that I cannot find a mistake.


edit: I got it to work using the `@QtCore.Slot 'decoration in this method. Thanks for the comment and response.

+1
source share
1 answer

You cannot call ordinary methods, only signals and slots. That is why it does not work for you. See the QMetaObject documentation for more details:

Invokes the member (signal or slot name) of obj. Returns true if the member can be called. Returns false if such an element is missing or the parameters do not match.

Try this decorator:

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

See the following documentation for more details, and this one :

Using QtCore.Slot ()

Slots are assigned and reloaded using the QtCore.Slot () decorator. Again, to define a signature, simply pass types such as the QtCore.Signal () class. Unlike the Signal () class, to overload a function, you do not pass all the options as a tuple or list. Instead, you should define a new decorator for every other signature. Below is an example below.

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

+2
source

All Articles