QMetaObject::invokeMethod 找不到方法 [英] QMetaObject::invokeMethod doesn't find the method

查看:298
本文介绍了QMetaObject::invokeMethod 找不到方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

输出为:

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

虽然我期待 beep.未调用该方法.

while I expected beep. The method was not invoked.

怎么了?看起来太简单了,我找不到错误.

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

我在方法上使用`@QtCore.Slot'装饰让它工作.感谢评论和回答.

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

推荐答案

您不能调用常规方法,只能调用信号和槽.这就是为什么它不适合你.有关详细信息,请参阅 QMetaObject 文档:

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:

调用对象 obj 上的成员(信号或槽名称).如果可以调用成员,则返回 true.如果没有这样的成员或参数不匹配,则返回 false.

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')
...

查看以下文档了解详情以及这个:

使用 QtCore.Slot()

Using QtCore.Slot()

使用装饰器 QtCore.Slot() 分配和重载插槽.同样,要定义签名,只需传递类似 QtCore.Signal() 类的类型.与 Signal() 类不同,要重载函数,您不必将每个变体都作为元组或列表传递.相反,您必须为每个不同的签名定义一个新的装饰器.下面的示例部分将使您更清楚.

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.

另一个不同之处在于其关键字.Slot() 接受名称和结果.result 关键字定义将返回的类型,可以是 C 或 Python 类型.name 的行为方式与 Signal() 中的相同.如果没有作为名称传递,那么新插槽将与正在装饰的函数具有相同的名称.

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.

这篇关于QMetaObject::invokeMethod 找不到方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆