2

I am attempting to use a QtCore.Signal to send a new instance of a class to the object's parent. What I am attempting to do is as follows, but fails with:

NameError: name 'myClass' is not defined

class myClass(QtGui.QMainWindow):
    mySignal = QtCore.Signal(myClass)
    def __init__(self, parent=None):
        super(myClass, self).__init__(parent)

    def create_new_self(self):
        newSelf = myClass(self.parent())
        self.mySignal.emit(newSelf)

class myParentClass(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(myParentClass, self).__init__(parent)
        myNewClass = myClass(self)
        myNewClass.mySignal.connect(self.handle_my_signal)

    @QtCore.Slot(myClass)
    def handle_my_signal(self, newMyClass):
        newMyClass.mySignal.connect(self.handle_my_signal)
        self.listOfClasses.append(newMyClass)

By Changing the line:

mySignal = QtCore.Signal(myClass)

to

mySignal = QtCore.Signal(QtGui.QMainWindow)

seems to fix the error, but I am not sure if that is the correct way to do something like this, other than it probably isn't the best way to do what I am doing.

1 Answer 1

2

The typical way to do this would be to define the signal as

mySignal = QtCore.Signal(object)

since you wish to emit an instance of a class.

EDIT: If you wish to work around the circular dependency, you can place your signal in a wrapper class. E.g.

class myClass(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(myClass, self).__init__(parent)
        # Note: argument of "self" is to parent the object
        self.signalwrapper = MyWrapper(self)

    def create_new_self(self):
        newSelf = myClass(self.parent())
        self.signalwrapper.mySignal.emit(newSelf)

class MyWrapper(QtCore.QObject):
    mySignal = QtCore.Signal(myClass)
4
  • Is there a benefit to using more precise typing? So for example, if I am passing and int through a signal, I could also just use object since (nearly?) everything is "first-class"? Jun 3, 2016 at 11:30
  • 1
    @busfault If you're also defining your slots using pyqtSlot, being more specific in your types may allow you to catch errors. It can also be good if you're sending signals across threads, and the documentation indicates that it can be more memory efficient. Jun 3, 2016 at 17:15
  • @BrendanAbel I am using PySide which doesn't have pyqtSlot, only QtCore.Slot() does pyqtSlot offer the ability to catch "type" errors? An Issue I ran into once was I changed a Signal definition and not the corresponding Slot and suddenly the Slot was no longer getting the Signal. Jun 3, 2016 at 17:21
  • 1
    @busfault I am unsure of the technicalities of this, but in case it matters, I've provided an additional solution in my answern which allows you to specify the type correctly. Jun 4, 2016 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.