Wednesday, 29 February 2012

PyQt Signals and Slots

To capture events generated by GUI-elements in PyQt the signal and slots mechanism from Qt is used:

Signals: signals are emitted when a user interacts with a Qt widget (e.g. the user clicks a button).

Slots: slots can be connected to signals and act upon receiving the signal.

It is possible to connect multiple slots to one signal.

With PyQt there are three different ways one can connect a slot to a signal:

The classic way:

To connect a slot to a signal in the classical Qt way the QObject's connect method is used:

object.connect(signal_widget, signal, slot_widget, slot)

import sys
from PyQt4 import QtCore, QtGui

def slot_function():
    """This function will be triggered when the button is clicked."""
    print("The function was triggered.")

if __name__ == '__main__':
    application = QtGui.QApplication(sys.argv)
    window = QtGui.QWidget()
    window.size = QtCore.QSize(20, 20)
    # This button will emit the signal when it is clicked.
    signal_button = QtGui.QPushButton("Click here", window)

    # Connect the button-click signal to the slot_function we defined earlier.
    QtCore.QObject.connect(signal_button, QtCore.SIGNAL("clicked()"), slot_function)

The signal that is emitted is specified via the QtCore.SIGNAL()-method. In this example no slot is specified, because our self-defined function shall be executed when the signal is received. By specifying a slot it is possible to declare which slot the slot_object will execute upon receiving the signal.

There are, however two more ways that were introduced by PyQt: New style signal and slots

New style signals and slots:

Connecting a signal to a slot with the new style system is easy and only uses the object that will emit the signal:


In the example above the QtCore.QObject.connect(...)-method would be replaced with:


Moreover it is also possible to autoconnect slots using Qt - this is especially helpful when using the Qt-Designer, but it can easily be activated by hand as well.

Autoconnecting slots and signals:

However, it will only work when the slots that shall be autoconnected are either defined in the same object that will emit the signal or in a child-object.

To autoconnect the objects the slots need to adhere to the following form:

on_<object name>_<signal name>(<signal parameters>)

To adjust the example to this signals/slots connection the MainWindow containing the button and the slot-function are moved into a class:

import sys
from PyQt4 import QtCore, QtGui 
class MainWindow(QtGui.QMainWindow):
    """A new window for the application."""
    def __init__(self):
        super(MainWindow, self).__init__()
        self.size = QtCore.QSize(20, 20)
        # This button will emit the signal when it is clicked.
        self.signal_button = QtGui.QPushButton("Click here", self)
        # This name is used to identify the  in the function.
        # This will wire up the signals and slots depending on names.

    def on_signal_button_clicked(self):
        """This function will be triggered when the button is clicked."""
        print("The function was triggered.")

def main():
    """docstring for main"""
    application = QtGui.QApplication(sys.argv)
    window = MainWindow()

if __name__ == '__main__':

This is all there is to autoconnecting slots and signals - when using the Qt Designer to create GUIs all the setup (setting an object name and using connectSlotsByName) are called inside the generated code of pyuic4 so you don't have to do this yourself.

Tuesday, 28 February 2012

Find files by extension on Windows and Linux

Using Windows at work I am kind of forced to find new ways to do familiar things:
today I needed to find a way to only find files with certain extensions.
Doing this on a Unix-based system is pretty easy when using the command line and the find-command:
find . -name "*.ext1" -o -name ".ext2"
find . -regex ".*\(ext1\|ext2\)"
However, Windows does not contain the standard Linux tools, so I had the chance to try the same in the Powershell and succeeded with the following command:
Get-ChildItem -Include @("*.ext1", "*.ext2") -Rec
Get-ChildItem takes a number of strings it matches again that can be passed with the @(string, ...) construct and the -Rec flag makes Get-ChildItem also search in sub-directories.
For anyone not wanting to use the shell, to find files with a certain extension in the Windows Explorer simply use the search toolbar and the following construct:
typ:=.ext1 OR typ:=.ext2
Make sure to capitalize the OR, or it will not work.
Moreover, the = sign before the dot makes sure that the extension is matched exactely, whereas using a * would also match partial matches - e.g.: *.h would also match all files that end in .html, whereas =.h only matches .h files.