'PySide.QtCore.Signal'对象没有属性'connect'

时间:2016-04-11 21:37:33

标签: python pyside

我正在使用带有Pyside 1.2.4和PyQt 4.8.7的Python 3.4,当我尝试将信号连接到插槽时,它说:

  

'PySide.QtCore.Signal'对象没有属性'connect'

我正在使用MVC:

型号:

from PySide.QtCore import Signal
class Model(object):

    def __init__(self):
        self.updateProgress = Signal(int)

控制器:

class Controller(QWidget):
"""
MVC Pattern: Represents the controller class

"""
def __init__(self, parent=None):
    super().__init__(parent)
    self.model = Model()
    self.model.updateProgress.connect(self.setProgress)

当我在Pycharm中查找Class时,通过按住CTRL并单击Signal类,它看起来如下所示:

class Signal(object):
""" Signal """
def __call__(self, *args, **kwargs): # real signature unknown
    """ Call self as a function. """
    pass

def __getitem__(self, *args, **kwargs): # real signature unknown
    """ Return self[key]. """
    pass

def __init__(self, *args, **kwargs): # real signature unknown
    pass

@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
    """ Create and return a new object.  See help(type) for accurate signature. """
    pass

def __str__(self, *args, **kwargs): # real signature unknown
    """ Return str(self). """
    pass

...根据PySide文档,实际上应该有 connect disconnect emit 的方法,可在以下位置找到:

  

https://srinikom.github.io/pyside-docs/PySide/QtCore/Signal.html#PySide.QtCore.Signal.connect

感谢您的提前帮助

3 个答案:

答案 0 :(得分:11)

The signal must be defined on the class, not the instance. The class must be a subclass of QObject, or be a mixin of such a class. So either of:

class Model(QtCore.QObject):
    updateProgress = Signal(int)

or:

class Mixin(object):
    updateProgress = Signal(int)

class Model(Mixin, QtCore.QObject):
    pass

答案 1 :(得分:2)

除了ekhumoro的回答,带有信号的类也需要调用super().__init__()。忘记这样做可能导致相同的错误。

class Model(QtCore.QObject):
    updateProgress = Signal(int)

    def __init__(self):
        super().__init__()  # This is required!
        # Other initialization...

答案 2 :(得分:0)

具体工作示例:

class QWorker(QRunnable, QObject):
    started = Signal()
    finished = Signal()
    failed = Signal()

    def __init__(self, fn, *args, **kwargs):
        QObject.__init__(self)
        QRunnable.__init__(self)

        self.fn = fn
        self.args = args
        self.kwargs = kwargs

    def run(self):
        self.started.emit()
        try:
            self.fn(*self.args, **self.kwargs)
        except BaseException as e:
            self.failed.emit()
        else:
            self.finished.emit()

有趣的是,你必须执行QObject.__init__(self)
而不是 super(QObject, self).__init__(),那么它就起作用了