QMetaObject::invokeMethod找不到该方法

2024-06-24 13:20:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用QMetaObject::invokeMethod来调用一个对象的方法(稍后它将在另一个线程中运行,然后invokeMethod会派上用场)。我在python3.3上使用pyside1.2.1的qt4.8绑定。完整的例子是:

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)

输出是:

^{pr2}$

而我期望beep。未调用该方法。在

怎么了?看起来很简单,我找不到错误。在


编辑:我使用`@QtCore.槽“方法上的装饰。感谢你的评论和回答。在


Tags: 对象方法fromselfinitdefbeep线程
1条回答
网友
1楼 · 发布于 2024-06-24 13:20:06

不能调用常规方法,只能调用信号和插槽。这就是为什么它不适合你。有关它的详细信息,请参见QMetaObject documentation

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.

试试这个装饰师:

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

有关详细信息,请参见following documentation,以及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.

相关问题 更多 >