views:

278

answers:

3

I'm having issues with a custom signal in a class I made.

Relevant code:

self.parse_triggered = QtCore.pyqtSignal()

def parseFile(self):
    self.emit(self.parse_triggered)

Both of those belong to the class: RefreshWidget. In its parent class I have:

self.refreshWidget.parse_triggered.connect(self.tabWidget.giveTabsData())

When I try to run the program, I get the error:

AttributeError: 'PyQt4.QtCore.pyqtSignal' object has no attribute 'connect'

Help? Thanks in advance.

+3  A: 

I had the same exact problem as you.

Try moving

self.parse_triggered = QtCore.pyqtSignal()

out of your constructor but inside your class declaration. So instead of it looking like this:

class Worker(QtCore.QThread):
    def __init__(self, parent = None):
        super(Worker, self).__init__(parent)

        self.parse_triggered = QtCore.pyqtSignal()

It should look like this:

class Worker(QtCore.QThread):
        parse_triggered = QtCore.pyqtSignal()
        def __init__(self, parent = None):
            super(Worker, self).__init__(parent)

This might not be at all what you are looking for, but it worked for me. I switched back to old-style signals anyways because I haven't found a way in new-style signals to have an undefined number or type of parameters.

Joel Verhagen
It works for me. Good catch!
Kaleb Pederson
That did it! Thanks.
Dane Larsen
Can someone explain why this is?
Xiong Chiamiov
+1  A: 

Why do you connect directly to the signal, while you can do self.connect(widget, SIGNAL('parse_triggered()'), listener.listening_method)?

where self is, for example, the form itself, and may be the same as listener

Guard
Supposedly that is the "old way" of doing connect(). From what I've read, that method won't put up any errors even if they exist.
Dane Larsen
This is old-style signals and slots. Both work, but people tend to think the new-style is more elegant/pythonic.
Joel Verhagen
A: 

I have recently started working with PySide (Nokia's own version of PyQt), and saw the exact same behaviour (and solution) with custom new-style signals. My biggest concern with the solution was that using a class variable to hold the signal would mess things up when I have multiple instances of that class (QThreads in my case).

From what I could see, QtCore.QObject.__init__(self) finds the Signal variable in the class and creates a copy of that Signal for the instance. I have no idea what QObject.__init__() does, but the resulting Signal does proper connect(), disconnect() and emit() methods (and also a __getitem__() method), whereas the class Signal or standalone Signal variables created outside of a QObject-derived class do not have these methods and can't be used properly.

Jare