I’ve been using PyQt for about two years now. Mark Summerfield’s books on Qt, PyQt, and Python are and have been a great help. If you program in any area he’s written on, I highly recommend checking his work out. His PyQt and first Qt book are a bit dated, but provides an awesome introduction. The new Advanced Qt Programming is great in the areas it covers, but it’s expensive and given the size of Qt, can’t cover everything. Nevertheless, it has some great work on threading and other areas that are useful. His other book on Qt using C++, C++ GUI Programming with Qt4, is definitely dated but a wonderful introduction to Qt. If you’re programming Python and PyQt, it’s still easy enough to follow conceptually and most of it translates to Python.

N.B.: The rest of this post discusses Python 3 or v3-style PyQt programming, which uses built-in Python classes to replace many Qt classes. The most prominent of this is replacing instances of QSting with Python’s str. It is my understanding that this is supported in Python 2.x, so it should still be applicable.

There are two areas of PyQt, however, where Python excels beyond that of the pace with which it can be developed. I discovered PyQt’s signal/slot model before keyword arguments, but they are both gems. PyQt handles Qt-style properties in a manner very similar to Python’s properties with only minor differences. Because of that, they didn’t make my Favorite Little-Known PyQt Features list. First up is what the PyQt documentation calls new-style signals and slots. It’s elegant, clean and simple.

With completely C++-like “old style”–as the documentation calls it–signals and slots, connecting a QToolButton to a slot would look like this:

QObject.connect(self.startButton, SIGNAL('clicked()'), self, SLOT('startDownload()'))

If we throw in a little Python, it looks like this:

QObject.connect(self.startButton, SIGNAL('clicked()'), self.startDownload)

That’s a little clearer, but it’s not “Pythonic” (I really don’t like that term, but it’s true here). Here’s how it looks using new-style signals and slots:

self.startButton.clicked.connect(self.startDownload)

I’d call that a much cleaner way of doing things. We can take this a step further to support overloaded signals. Imagine a QComboBox with the currentIndexChanged() signal, which can have a QString (str in PyQt) or int parameter. Getting this to work is pretty simple, too: we index the parameter type. For example:

someComboBox.currentIndexChanged[int].connect(self.fooMethod)

PyQt treats the currentIndexChanged() signal like a dict to pick which of the two signal signatures to connect, using the class type as a key.

Next up is using keyword arguments for Qt properties and signals during QObject construction. Consider how our imaginary startButton QToolButton would be constructed, C++ style:

self.startButton = QToolButton(QIcon(‘./images/start.png), ‘Start’, self)
self.startButton.setObjectName(‘startButton’)
self.startButton.setEnabled(False)
self.startButton.setCheckable(False)
self.startButton.setToolTip(‘Starts the download.’)
self.startButton.setIconSize(QSize(32,32)
self.startButton.clicked.connect(self.startDownload)

I threw in the signal connection for a reason, which I will explain in a moment. One thing that C++ features and Python lacks is method signature overloading, a necessary loss given Python’s dynamic nature and duck-typing. SIP transparently handles this for us, so PyQt still works nicely with Qt’s frequent function overloads. I mention this because of the first line, where I used the QToolButton(QIcon, QString, QObject) signature as opposed to QToolButton(QObject). It’s a small thing, but try implementing that in Python–it sucks. Consequently, some Python zealots would argue that it’s not very Pythonic. Anyway, let’s get fancy.

PyQt implements support for setting Qt properties during the Python __init__ call after creating the C++ object it wraps, and it does this via keyword arguments. To take advantage, we can just tail any property names as keywords during the constructor call. In fact, it’s even possible to completely exclude any non-keyword arguments, which I will do. Converting the above example to completely keyword argument-based __init__ call might look like this:

self.startButton = QToolButton(parent=self,
                               objectName=’startButton’,
                               text=’Start’,
                               icon=QIcon(‘./images/start.png’),
                               enabled=False,
                               toolTip=’Starts the download’,
                               iconSize=QSize(32, 32),
                               clicked=self.startDownload)

Boom. Using this method has a number of advantages. First and most obviously, it’s faster to type. Secondly, it clusters things a little more neatly and list-like, which makes it easier to read (and modify). Also, note the signal connection on the end. I haven’t tried to use keyword arguments for overloaded signals (as in the QComboBox example) before, and I doubt it would work. I should mention a caveat I’ve run into regarding signal connection by keyword. I usually lump all of my signal and slot connections in a method I also usually call connectSignals()–I’m tricky with names like that. Doing this makes it easy to see exactly what signals/slots are connected, modify them, and selectively disable if necessary for debugging. Having an extraneous signal connected elsewhere–even if it’s for a QPushButton connected to QDialog.close()–is a little out of place. If you lump your signal connections in the same way, be aware that connecting signals by keyword argument can become an issue.

All right, that’s it for now. Happy New Year.

Leave a Reply