PyQt signal styles¶
Newer versions of PyQt (starting with version 4.5) have support for two different styles of syntax for signals.
I prefer to use the new-style simply because it looks cleaner and closer to native Python. I don't like having to specify types and function calls in strings like:
This syntax seems to expose too much of the native C++ nature of Qt.
I've always used the new syntax:
This choice served me well until I started experimenting with
Qt Designer and pyuic4
to generate GUI code. The issue is that pyuic4 generates code with the
old-style signal syntax. I suppose this isn't a real problem since the whole
point of using GUI code generation tools is to avoid having to write and/or see
GUI code.
But, what happens when you want to manually disconnect a signal that is automatically hooked up by the auto-generated code?
I don't want to use this old-style syntax in my own code. So when I recently needed to disconnect a signal from auto-generated code I chose to use the new-style syntax like the following:
This looks correct, but I kept running into this error:
self.buttonBox.accepted.disconnect(self.accept)
TypeError: disconnect() failed between 'accepted' and 'accept'
What is going on here? I know the signal is connected by the auto-generated code!
I found the answer after a bit of searching around. The end result is that you cannot mix and match syntax styles on the same object. So, if a particular signal is connected with the old-style syntax then it must be disconnected with the old-style.
However, you can mix and match the syntax styles across different objects.
So to resolve my issue I have two choices:
-
Manually edit the auto-generated code (and remember to do so everytime it's regenerated)
-
Use one line of the old-style syntax in my hand-written code.
I chose the latter because it's the easiest and least error-prone solution.
This is probably something to keep in mind if you, like me, prefer the new-style syntax but still want to use Qt Designer and pyuic4.