gpt4 book ai didi

python - QMetaObject::invokeMethod 找不到该方法

转载 作者:行者123 更新时间:2023-12-01 05:16:22 29 4
gpt4 key购买 nike

我想使用QMetaObject::invokeMethod来调用一个对象的方法(稍后它会在另一个线程中运行,然后invokeMethod就派上用场了)。我在 Python 3.3 上使用 PySide 1.2.1 的 Qt 4.8 绑定(bind)。完整的例子是:

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)

输出是:

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

虽然我期待嘟嘟声。该方法未被调用。

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

<小时/>

编辑:我使用方法上的“@QtCore.Slot”装饰让它工作。感谢评论和回答。

最佳答案

您不能调用常规方法,只能调用信号和槽。这就是为什么它不适合你。请参阅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.

关于python - QMetaObject::invokeMethod 找不到该方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23127844/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com