PyQt5 Tutorial

This is an introductory PyQt5 tutorial with the goal of getting you started with the toolkit so that you can create functional UIs in Python.  While I can’t hope to cover all of Qt (it’s a huge library) hopefully I can give you enough of an introduction to be able to take the API docs and create the UI you imagine.  The tutorial code was written on a Windows machine with Python 3.6.1 and PyQt5.  You’ll need to be a competent coder with intermediate Python skills to follow along but there is nothing particularly difficult.

Over the course of the tutorial, we’re going to write a code editor with a heap of nice features like a tabbed interface and project navigation which is built in a progressive manner.   Later on, I’ll start to add ‘featurette’ articles to add extra features that don’t necessarily progress the tutorial but are nice little extras.

Installing PyQt5

You can install PyQt5 with pip.  On Windows –

pip3 install pyqt5

On Linux –

sudo pip3 install pyqt5

PyQt5 Modules

PyQt5 is organised into a number of modules, many of which are quite specific and not all that commonly used.  There are however a couple of modules you’ll use in pretty much every UI project –

  •  PyQt5.QtCore contains core Qt functionality including data types like date & time, threads & processes and the Qtobject which gives you access to enumerations for configuring various widgets and Qt objects.
  •  PyQt5.QtGui contains UI related objects that are not widgets – stuff like icons, images and UI events
  •  PyQt5.QtWidgets are the actual UI elements like layouts, and widgets
Qt5 Class Reference

The Qt5 class reference can be found here – it’s for C++ but the API is almost exactly with the notable exception of data types that have a direct Python equivalent – we’ll cover these as we come across them.

Table of Contents

  1. Scaffolding – basic PyQt5 project setup / resources
  2. Widgets – introduction to widgets in Qt – QPlainTextEdit / QTabWidget / QMainWindow
  3. Signals
  4. Toolbars, menus and actions
  5. MVC 1 – TreeView / FileSystemModel
  6. MVC 2 – Custom TreeView model