Chapter 6. Embedding Matplotlib in Qt 4

Chapter 6. Embedding Matplotlib in Qt 4

There are several GUI libraries available, and one of the most famous is Qt. In this book, we will use Qt 4, the latest major version of this library. Unless explicitly mentioned, when we write Qt, we are referring to Qt 4.

We will follow the same progression as in the GTK+ chapter, and we will present the same examples, but this time written in Qt.

We believe that this method will allow us to directly compare the libraries, and it has the advantage of not leaving the "How would I write something with library X?" question unanswered.

During this chapter, we will see how to:

  • Embed a Matplotlib Figure into a Qt widget

  • Embed a Figure and navigation toolbar into a Qt widget

  • Use events to update in real-time a Matplotlib plot

  • Use Qt Designer to draw a GUI and then use it with Matplotlib in a simple Python application

We begin by giving an introduction to the library.

Brief introduction to Qt 4 and PyQt4

Qt is a cross-platform application development framework, widely used for graphical programs (GUI) but also for non-GUI tools.

Qt was developed by Trolltech (now owned by Nokia), and it's probably best known for being the foundation of the K Desktop Environment (KDE) for Linux.

The Qt toolkit is a collection of classes to simplify the creation of programs. Qt is more than just a GUI toolkit; it includes components for abstractions of network sockets, threads, Unicode, regular expressions, SQL databases, SVG, OpenGL, and XML, as well as a fully functional web browser, a help system, a multimedia framework, and a rich collection of GUI widgets.

Qt is available on several platforms, in particular: Unix/Linux, Windows, Mac OS X, and also some embedded devices. As it uses the native APIs of the platform to render the Qt controls, applications developed with Qt have a look and feel which fits the running environment (without looking like aliens in it).

Though written in C++, Qt can also be used in several other programming languages, through language bindings available for Ruby, Java, Perl, and also Python with PyQt.

PyQt is a comprehensive set of Python bindings for the Qt framework. PyQt provides bindings for Qt 2 and Qt 3. PyQt4 is a separate set of bindings and covers the Qt 4 series of releases. We will use PyQt4 and references to PyQt should be considered to be referring to PyQt4.

PyQt brings together the Qt C++ cross-platform application framework and the cross-platform interpreted language, Python. An application written in Qt and PyQt often runs unchanged on all the supported platforms.

The Qt components are mapped to several Python submodules (where PyQt4 is the main module), and the most important are:

  • The QtCore module, which contains the core non-GUI classes (for example, the event loop).

  • The QtGui module, which contains the majority of the GUI classes.

  • QtOpenGL, QtScript (JavaScript support), QtSql (SQL databases support), QtSvg (SVG file support), QtTest (unit testing support), QtXml (XML support), QtNetwork (for network programming), and several others. Note how many of these submodules provide functionalities that are already present in Python standard library.