

Create GUI Applications with Python & Qt5
The hands-on guide to making apps with Python
Martin Fitzpatrick
Version 4.0, 2020-09-12

Table of Contents
Introduction
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê1
1. A very brief history of the GUI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê3
2. A bit about Qt
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê5
3. Thankyou
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê7
4. Copyright. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê8
Basic PyQt5 Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê9
5. My first Application
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê10
6. Signals & Slots
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê21
7. Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê32
8. Layouts
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê63
9. Actions, Toolbars & Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê91
10. Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê114
11. Windows
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê130
12. Events
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê140
Qt Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê149
13. Installing Qt Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê150
14. Getting started with Qt Designer
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê154
15. The Qt Resource system
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê173
Theming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê183
16. Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê184
17. Palettes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê187
18. Icons
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê198
19. Qt Style Sheets (QSS). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê206
Model View Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê260
20. The Model View Architecture — Model View Controller
. . . . . . . . . . . . . . . Ê261
21. A simple Model View — a Todo List
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê264
22. Tabular data in ModelViews, with numpy & pandas . . . . . . . . . . . . . . . . . . Ê280
23. Querying SQL databases with Qt models
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê305
Further PyQt5 Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê336
24. Extending Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê337
25. Routing
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê349
26. Working with command-line arguments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê354
27. System tray & macOS menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê359
28. Enums & the Qt Namespace
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê369
Custom Widgets
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê379

29. Bitmap Graphics in Qt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê380
30. Creating Custom Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê412
Concurrent Execution
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê447
31. Introduction to Threads & Processes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê448
32. Using the thread pool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê454
33. Threading examples
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê463
34. Running external commands & processes . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê528
Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê538
35. Plotting with PyQtGraph
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê539
36. Plotting with Matplotlib
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê560
Packaging & Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê576
37. Packaging with fbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê577
Example applications
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê597
38. Mozzarella Ashbadger
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê598
39. Moonsweeper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê617
Appendix A: Installing PyQt5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê640
Appendix B: Translating C++ Examples to Python
. . . . . . . . . . . . . . . . . . . . . . . . . . Ê643
Appendix C: PyQt5 and PySide2 — What’s the difference?
. . . . . . . . . . . . . . . . . Ê655
Appendix D: What next?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê666
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Ê667

Introduction
If you want to create GUI applications with Python it can be tricky to know
where to start. There are a lot of new concepts you need to understand to get
anything to work. But, like any coding problem, the first step is learning to
approach the problem in the right way. In this book I take you right from the
basic principles of GUI development to creating your own, fully functional,
desktop apps with PyQt5.
The first edition of this book was released in 2016. Since then it has been
updated 4 times, adding and expanding chapters in response to reader
feedback. There are more PyQt5 resources available now than when I started,
but there is still a shortage of in-depth, practical guides to building complete
apps. This book fills that gap!
The book is formatted as a series of chapters exploring different aspects of
PyQt5 in turn. They are arranged to put the simpler chapters toward the
beginning, but if you have specific requirements for your project, don’t be
afraid to jump around! Each chapter will guide you through learning the
fundamental concepts before taking you through a series of coding
examples to gradually explore and learn how to apply the ideas yourself.
You can download
source code and resources for all examples in this book.
But don’t be tempted just to read the code and move on — you will learn
much more if you experiment along the way.
It is not possible to give you a complete overview of the Qt system in a book
of this size, so there are links to external resources — both on the
LearnPyQt.com website and elsewhere. If you find yourself thinking "I
wonder if I can do that?" the best thing you can do is put this book down,
then go and find out! Just keep regular backups of your code along the way
so you always have something to come back to if you royally mess it up.
1