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

标签 python qt pyside qtcore qt-signals

我想使用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/

相关文章:

qt - 无法访问驻留在同一目录中的 QML 单例

python - 将 QmouseEvent 转换为 QGraphicsSceneMouseEvent 以进行拖动移动

python - Qt QGraphicsDropShadowEffect 没有显示

python - 无法将 Django mongodb 引擎与 Django 一起使用?

python - 无法使用 uwsgi 和 Docker 查看我的简单 Flask 应用程序

c++ - Qt 在 Linux 上不显示 PNG 图像

Java 相当于 QT 中的信号和槽

c++ - 我怎么知道鼠标是否在小部件上?

python - python seaborn 图中的图例标签不正确

python - 学习Python的交互式教程