使用* args创建通用信号

时间:2015-05-26 20:58:35

标签: python multithreading python-3.x signals qobject

我试图创建一个通用函数调用类,目的是从主线程外部调用GUI函数。该类有一个信号和两个函数:一个被调用以发出信号并从侧面线程运行。另一个运行实际功能,并连接到主线程中的信号。

为了保持这个类的通用性,我希望能够传递任意数量的参数,正如通常使用* args或** kwargs所做的那样。但是在初始化Signal I时需要定义传递的参数。

这是我的通用函数调用者(为清晰起见,代码被剥离):

from PySide.QtCore import Signal

class FunctionCaller(QObject):
    _request = Signal(int)

    def __init__(self, function=str):
        super().__init__()
        self.function = function
        self._request.connect(self._do_function)

    @Slot(*args)
    def _do_function(self, *args):
        """This runs in the GUI thread to handle the thing."""
        self.function(*args)

    def doFunction(self, *args):        
        self._request.emit(*args)

这是主线程中出现的内容:

functionCaller = FunctionCaller(function = someGUIfunction)
foo = functionCaller.doFunction # foo is a global variable

我在这里调用侧线程中的函数:

foo(0xc000, 'string')

运行带有3个参数的函数时得到的错误:_request(int) only accepts 1 arguments, 3 given!

有没有办法做到这一点,或者这个函数必须不像我希望的那样通用,只适用于一组数量和类型的参数?

0 个答案:

没有答案