我正在尝试为我的 PySide GUI 库创建简单、通用的方法调用:
class GUI(QApplication):
def __init__(self, *args, **kwargs):
super(GUI, self).__init__()
self.gui_signal = Signal()
self.gui_signal.connect(self.method_call)
获取对象、方法并使用args调用object.method的槽:
@Slot()
def method_call(self, obj, method_name, *args, **kwargs):
method = getattr(obj, method_name)
if method:
return method(*args, **kwargs)
else:
return None
这应该发出那个棘手的插槽:
def anymethod(self, obj, method_name, *args, **kwargs):
self.gui_signal.emit(obj, method_name, *args, **kwargs)
这是另一个工作线程,例如更新进度条
class MyApp(object):
...
def update_progress(self):
perc = (self.done * 100) / self.total
self.gui.anymethod(self.progressBar, 'setValue', int(perc))
我发现这种方法不起作用,并且参数传递的方式错误。我应该做什么来修复?
最佳答案
信号不应定义为类的成员,而应定义为类的属性。另一方面,您必须指明签名,最后,字典不支持,因此您必须将 *args
、**kwargs
转换为元组。
from PySide import QtCore, QtGui
class GUI(QtGui.QApplication):
gui_signal = QtCore.Signal(object, str, tuple)
def __init__(self, *args, **kwargs):
super(GUI, self).__init__([])
self.gui_signal.connect(self.method_call)
@QtCore.Slot(object, str, tuple)
def method_call(self, obj, method_name, data):
if hasattr(obj, method_name):
method = getattr(obj, method_name)
args, kwargs = data
if hasattr(method, '__call__'):
method(*args, **kwargs)
def anymethod(self, obj, method_name, *args, **kwargs):
self.gui_signal.emit(obj, method_name, (args, kwargs))
class MyApp(object):
def __init__(self):
self.gui = GUI()
self.progressBar = QtGui.QProgressBar(maximum=100)
self.progressBar.show()
self.total = 200
self.done = 100
QtCore.QTimer.singleShot(300, self.update_progress)
def update_progress(self):
perc = (self.done * 100) / self.total
self.gui.anymethod(self.progressBar, 'setValue', int(perc))
def run(self):
return self.gui.exec_()
if __name__ == '__main__':
import sys
app = MyApp()
sys.exit(app.run())
关于python - PySide *任何*方法调用 - 信号和插槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53909767/