Skip to content

Commit fc49753

Browse files
add presentation notes for: 2023-05-23 "GUIs in PyQt5"
1 parent 5390a25 commit fc49753

File tree

1 file changed

+263
-0
lines changed

1 file changed

+263
-0
lines changed

Diff for: _posts/2023-05-23-GUIs-in-PyQt5.md

+263
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
# Ramin Honary
2+
3+
- Worked at Cross Compass since 2018
4+
- Platform Team development, full stack
5+
- Worked primarily on Manufacia
6+
- Currently on working on ML-Platform
7+
8+
## Why I use PyQt
9+
10+
- Developed some Python scripts that used **OpenCV**
11+
- "Pattern Matching Kit" (will demo)
12+
- "Image Cropping Kit" (in progress)
13+
- OpenCV provides a API for GUI tools based on PyQt
14+
- Decided to use PyQt directly instead of OpenCV specific API
15+
16+
# Part 1: What is Qt
17+
18+
Qt is a cross-platform desktop computing GUI framework
19+
20+
- Free software (LGPL)
21+
- Option of buying commercial licenses
22+
- Develops native apps for
23+
- Windows, MacOS
24+
- Linux
25+
- Core of the KDE Plasma desktop environment
26+
27+
## Qt History
28+
29+
- Originally developed for Linux
30+
- Actively maintained since 1995
31+
- Python bindings maintained since 1998
32+
- Changed corporate ownership several times (Trolltech, Nokia, now
33+
"The Qt Company").
34+
- (pronounced "Cute")
35+
36+
## Model-View-Controller (MVC)
37+
38+
- Somewhat different from Model-View-Update (React, Vue)
39+
- Must be careful about model state
40+
- **My opinion:** best used for small apps, developing larger apps
41+
require paying for professional tooling
42+
43+
## Programming languages
44+
45+
- Primarily C++
46+
- has Python and JavaScript wrappers
47+
- Qt Creator (optional)
48+
- Visual GUI builder tools
49+
- QML (optional)
50+
- an XML schema for declarative GUI
51+
52+
## Proprietary developer tools (C++ and QML only)
53+
54+
- Qt Design Studio
55+
- Qt Quality Assurance tools
56+
- Static analysis (e.g. code coverage)
57+
- Automated GUI testing
58+
- *(Third-party apps that were acquired by The Qt Company)*
59+
- *(I use none of the Qt professional tools, just Emacs)*
60+
61+
## Qt Designer (free)
62+
63+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/qt-designer-screenshot.png)
64+
65+
## Qt Design Studio (1)
66+
67+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/qt-design-studio_image-png-Dec-10-2020-04-03-18-99-PM.png)
68+
69+
## Qt Design Studio (2)
70+
71+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/qt-design-studio_connection-editor.png)
72+
73+
## Froglogic "Coco" code covereage analysis
74+
75+
(Originally a third-party tool acquired by *The Qt Company*)
76+
77+
![(Source froglogic.com)](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/froglogic-coco_coverage-770x542.png)
78+
79+
## Provides many extensions
80+
81+
- OpenGL views (built-in)
82+
- 3D widgets, modeling controlls
83+
- SQL database integration, ORM
84+
- Multimedia, AV codecs and playback
85+
- WebKit web views
86+
- Sensors, GPS, touch screen, BlueTooth, game pads, etc.
87+
88+
# KDE Plasma apps showcase
89+
90+
Brief photo slideshow of KDE apps:
91+
92+
- <https://apps.kde.org/>
93+
94+
## KDE Plasma: desktop environment for Linux
95+
96+
![(source OMGBuntu)](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/plasma-5_omgbuntu.jpg)
97+
98+
## Calindori: calendar app
99+
100+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/calindori_screenshot.png)
101+
102+
## KCalc: simple desktop calculator
103+
104+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/kcalc_statistics-screenshot.png)
105+
106+
## Dolphin: extensible file browser
107+
108+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/dolphin_pop-up-terminal-screenshot.png)
109+
110+
## "Kig" high-school geometry learning tool
111+
112+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/kig_screenshot.png)
113+
114+
## KWave: waveform visualizer
115+
116+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/kwave_screenshot.png)
117+
118+
## Marble: world atlas app
119+
120+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/marble_screenshot.png)
121+
122+
## KMPlot: graphing calculator
123+
124+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/kmplot_screenshot.png)
125+
126+
## Calligra: like Microsoft Office suite
127+
128+
!["Plan" app](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/calligra-suite_plan_screenshot.png)
129+
130+
## Okular: PDF reader with annotation tool
131+
132+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/okular_annotation-screnshot.png)
133+
134+
## KEXI: like Microsoft Access
135+
136+
![Query designer](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/kexi_3-1-query-designer_screenshot.png)
137+
138+
## KEXI: like Microsoft Access
139+
140+
![Form builder](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/kexi_3-0-gui_screenshot.png)
141+
142+
## KRWard: IDE for R Programming Language
143+
144+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/rkward_r-lang-frontend_screenshot.png)
145+
146+
## KLabPlot: Matlab-like app
147+
148+
![plots example](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/klabplot_basic-plots-linux-screenshot.png)
149+
150+
## KLabPlot: Matlab-like app
151+
152+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/klabplot_space-debris-linux-screenshot.png)
153+
154+
## KdenLive: non-linear video editor
155+
156+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/kdenlive-screenshot.png)
157+
158+
# Part 2
159+
160+
Basics of GUI app development in Python
161+
162+
## PyQt: Python Bindings
163+
164+
- Maintained by "Riverbank Computing" company, UK
165+
- Non-trivial Python bindings
166+
- Converting Python data to C++ native data
167+
- Contending with Python garbage collection
168+
- Must make callbacks written in Python callable from C++
169+
- Mechanisms for Python inhereitence and method overriding to work
170+
the same as in C++
171+
- Providing PEP 484 type hinting for all APIs
172+
- <https://www.riverbankcomputing.com/static/Docs/PyQt5/index.html>
173+
174+
## PyQt: Python Documentation
175+
176+
- Most documentation written by third parties
177+
- Easiest to read C++ documentation and hand-translate examples to
178+
Python.
179+
- Python Wiki: <https://wiki.python.org/moin/PyQt/>
180+
- Book: Michael Herrmann, "*Python and Qt: the best parts*"
181+
- *(I was able to learn from free online resources.)*
182+
183+
## Building blocks (1): widges, events, properties
184+
185+
- **Widgets:** tab bars, scroll bars, push buttons, list views,
186+
pull-down menus
187+
- **Event handlers:** callback functions responding to *primitive*
188+
mouse/keyboard events.
189+
- **Properties:** fields with getters and setters, changes
190+
automatically updates widget drawing on screen
191+
- e.g. a text fields current string
192+
193+
## Building blocks (2): signals, slots, connections
194+
195+
- **Signals:** complex combination of primitive events that trigger
196+
widget-specific actions
197+
- e.g. when list view item is double-clicked
198+
- **Slots:** public methods used as callbacks, can be "connected" to
199+
signals to be triggered on certain events.
200+
- **Connections:** are an object that maintain a link between a signal
201+
and slot. Can be disconnected/deleted
202+
- Using these building blocks, simple interfaces can be designed
203+
without the need for custom widgets.
204+
205+
## Object oriented programming: method overriding
206+
207+
- When you want to combine multiple widgets into a single more complex
208+
widget.
209+
- Signals and slots are usually not enough.
210+
- A custom widget inherits from a parent widget (e.g. `QWidget` or
211+
`QListView`)
212+
- Override signals and slots to customize behavior (e.g. update state
213+
variables)
214+
215+
<https://doc.qt.io/qt-5/qtwidgets-index.html>
216+
217+
## Declaring your own reusable widget
218+
219+
- Most code occurs in `__init__()` constructor
220+
- Create sub-widgets and layout
221+
- Place sub-widgets into layout object:
222+
- `QBoxLayout`
223+
- `QFormLayout`
224+
- `QGridLayout`
225+
- `QStackedLayout`
226+
- Define slots to update model: `@pyqtSlot()` decorator
227+
- Connect child widget signals to slots
228+
229+
## The **Graphics View Framework**
230+
231+
- Interactive 2D visualizations
232+
- Arbitrarily sized canvas (`QGraphicsScene`)
233+
- Easily renders large number of objects (`QGraphicsItem`)
234+
- Graphics view widget provides (`QGraphicsView`)
235+
- translating (with scroll bars)
236+
- scaling
237+
- rotation
238+
239+
<https://doc.qt.io/qt-6/graphicsview.html>
240+
241+
## Diagram Scene Example (tutorial)
242+
243+
![](../../../assets/images/2023-05-23-GUIs-in-PyQt5-pics/diagramscene.png)
244+
245+
<https://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html>
246+
247+
## Pattern Matching Tool Demo
248+
249+
I created an app to run a pattern matching algorithm using OpenCV to
250+
select parts of a "target" image that are similar to a "pattern" image.
251+
252+
## Conclusion
253+
254+
- Large improvement for little effort
255+
256+
- It requires relatively few lines of code to create a small,
257+
single-window GUI.
258+
259+
- Consider using if your project could be improved with simple
260+
interactive widgets.
261+
262+
- **The ML Platform team is interested in *your* reusable Qt
263+
Widgets\!**

0 commit comments

Comments
 (0)