我试图通过PySide2中的QMetaObject.invokeMethod调用QObject的插槽。插槽需要一个应包装在QGenericArgument中的参数。 QtCore.QGenericArgument总是抱怨提供给它的值。
在传统的C ++中,它是用Q_ARG完成的,而我找不到PySide2。在Qt示例中找不到任何最小样本。我在互联网上发现的唯一东西是this script,它也抱怨参数值
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QObject, QMetaObject, QThread, Signal, Slot, QGenericArgument, Qt
class TestMeta(QObject):
def __init__(self, parent=None):
super().__init__(parent)
@Slot(int)
def method(self, name):
print(QThread.currentThread())
print ("Method is called with argument {}".format(name))
import sys
if __name__ == "__main__":
app = QApplication(sys.argv)
test_meta = TestMeta()
thread = QThread()
test_meta.moveToThread(thread)
thread.start()
test_meta.method(12)
print(QThread.currentThread())
QMetaObject.invokeMethod( test_meta, "method", QGenericArgument('int', 12));
sys.exit(app.exec_())
对于上面的代码,它会显示以下错误消息
QMetaObject.invokeMethod( test_meta, "method", QGenericArgument('int', 12));
ValueError: 'PySide2.QtCore.QGenericArgument' called with wrong argument values:
PySide2.QtCore.QGenericArgument('int', 12)
Found signature:
PySide2.QtCore.QGenericArgument(str = None, int = None)