Tri*_*ion 6 python qt pyside qt-signals qtcore
这是QMetaObject :: invokeMethod没有找到方法的后续内容.调用没有参数的方法有效.但是将前一个问题扩展到带参数的方法会让我再次陷入失败.
请参阅Python中的以下示例脚本:
from PySide import QtCore
class Example(QtCore.QObject):
def __init__(self):
super().__init__()
@QtCore.Slot()
def dup(self):
beep('dup-class')
@QtCore.Slot(str)
def beep(self, text):
print(text)
@QtCore.Slot()
def dup(self):
beep('dup-local')
@QtCore.Slot(str)
def beep(text):
print(text)
if __name__ == '__main__':
QtCore.QMetaObject.invokeMethod(None, 'dup')
QtCore.QMetaObject.invokeMethod(None, 'beep', QtCore.Qt.AutoConnection, QtCore.QGenericArgument('text', 'beep-local'))
print('now some classy trials')
t = Example()
QtCore.QMetaObject.invokeMethod(t, 'dup')
QtCore.QMetaObject.invokeMethod(t, 'beep', QtCore.Qt.AutoConnection, QtCore.QGenericArgument('text', 'beep-class'))
QtCore.QMetaObject.invokeMethod(t, 'beep', QtCore.Qt.AutoConnection, QtCore.QGenericArgument('self', t), QtCore.QGenericArgument('text', 'beep-class-b'))
Run Code Online (Sandbox Code Playgroud)
在Windows 7和Ubuntu 14.04上使用PySide 1.2.1和Python 3.3的输出也是:
now some classy trials
dup-class
QMetaObject::invokeMethod: No such method Example::beep(text)
QMetaObject::invokeMethod: No such method Example::beep(self,text)
Run Code Online (Sandbox Code Playgroud)
这意味着对本地方法的invokeMethod调用以静默方式失败.只有对Example:dup()的调用给出了预期的输出.获得示例的两个试验:beep(str)工作失败,尽管失败消息给出了实际应该存在的方法签名.
我在PySide邮件列表上提出了这个问题的早期版本,但没有回答.
问题:如何QMetaObject::invokeMethod使用Python Qt绑定中的参数调用local和class方法(最好是在PySide中)?
编辑:顺便说一句:如果一个人知道什么Signal:emit(...)或QtCore.QTimer.singleShot(0, ...)做什么,这也可以帮助.毕竟所有这些不同的方法具有非常相似的效果.
EDIT2:
使用'QString'as参数名称时,警告消息会消失,但Python会因为segfaults而整体失败.它可能是PySide的实现错误.规则似乎是必须在invokeMethod中提供Qt-C++类型的参数,在Slots中提供Python类型.
from PySide import QtCore
class Example(QtCore.QObject):
def __init__(self):
super().__init__()
@QtCore.Slot(str)
def beep(self, text='default'):
print(text)
if __name__ == '__main__':
app = QtCore.QCoreApplication([])
e = Example()
QtCore.QMetaObject.invokeMethod(e, 'beep', QtCore.Qt.AutoConnection, QtCore.QGenericArgument('QString', 'beep'))
QtCore.QTimer.singleShot(1000, app.quit)
app.exec_()
Run Code Online (Sandbox Code Playgroud)
对于仍然对此感兴趣的人:
从版本1.2.4开始,PySide的错误在于它没有包装Q_ARG和Q_RETURN_ARG宏.相反,它不明智地包装QGenericArgument和QGenericReturnArgument类,它们是内部帮助程序类,并不意味着直接实例化.因此,使用参数调用插槽总是会导致分段错误.
相比之下,PyQt 确实包装宏而不是类,并且不会遇到相同的问题.