PyQT-Introduction

PyQT Overview and Set Up

PyQT Overview and Set Up

What is PyQT?

PyQt is a set of Python bindings for Qt application framework for building traditional Graphical User Interface (GUI) desktop applications
Qt (pronounced "cute") framework is a widget toolkit for creating GUIs as well as cross-platform applications that run on various software and hardware platforms such as Linux, Windows, macOS, Android or embedded systems with little or no change in the underlying codebase while still being a native application with native capabilities and speed. [Reference: Qt @wikipediq]
Qt includes powerful features for networking, threads, regular expressions, SQL databases, SVG, OpenGL, XML, and many other
PyQt is available in two editions:
PyQt4: built against Qt 4.x and 5.x
PyQt5: built against only for Qt 5.x (this is the edition used in the course).
PyQt5 is compatible with Windows, Unix, Linux, macOS, iOS, and Android.
As PyQt alternatives for building GUIs in Python, there are Tkinter, wxPython, PySide2

PyQT5 Installation

As python package from PyQt5 Project which will install the GPL version of PyQt5 (the recommended way)

				# on activated virtual environment:
				pip install PyQt5

				# check install info:
				pip show PyQt5
			
Reference: Installing PyQt5

On Linux/Ubuntu, if you have problems with standard installation, you can use your software package manager. Note that this will make a system-wide install.

				sudo apt install python3-pyqt5
			

Test the installation

Create the pyqt_test.py file and run it.

			import sys

			from PyQt5.QtWidgets import QApplication,QWidget,QLabel,QPushButton

			app = QApplication(sys.argv)

			window = QWidget()
			window.setWindowTitle('PyQt5 App Works')
			window.setGeometry(100, 100, 500, 500)

			window.show()
			sys.exit(app.exec_())
		

You should see a window like:
PyQT5AppWorks.png

Qt Tools: QtDesigner

QtDesigner is a Qt tool that provides a what-you-see-is-what-you-get (WYSIWYG) user interface which speeds the creation of GUIs for PyQt applications
QtDesigner creates .ui files (which contains an XML description of the GUI).
I.e., QtDesigner is platform and programming language independent.
Reference: Qt Designer Manual
./images/QtdesignerStart.png

QtDesigner Installation

Qt Designer normally ships as a part of Qt Creator. This is Qt's official editor and lets you do a lot more than just graphically design user interfaces. It is a full-fledged and very powerful C++ IDE. But For PyQT development you don't need the whole Qt Creator (which is huge).
You can install only the Qt Designer in two ways:

As python package pyqt5-tools (the recommended way):

				# make sure you're on activated virtual environment:
				pip install pyqt5-tools

				# check install info:
				pip show pyqt5-tools
			
Or as standalone application for Windows/MacOS (use it only if you have problems with python packages):
Install Qt Designer on Windows or Mac
On Linux/Ubuntu you can use your software package manager:

				sudo apt-get install qttools5-dev-tools
				sudo apt-get install qttools5-dev
			

QtDesigner - "HelloWorld" GUI

Start QtDesigner:
If you installed QtDesigner as Python package, just write in your activated virt. environment

				pyqt5-tools designer
			
If you have installed the QtDesigner as standalone app, you should have an icon for it
Now you can create your first "HelloWorld" app GUI:
./images/QtDesignerHelloWorld.gif

Qt Tools: pyuic

PyUIC (Python User Interface Compiler) is a Qt user interface compiler for Python
The pyuic reads a user interface definition file (.ui) in XML as generated by Qt Designer and creates corresponding Python class
Reference: pyuic5
Note, that pyuic5 is included in the pyqt5-tools, so you do not need to install it. Just use it.
Let's compile our .ui file created by QtDesigner into Python code:

			pyuic5 helloWorld.ui -o helloWorld.py
		

Setup PyCharm for PyQt5

Setup PyCharm for PyQt5

PyCharm - Basic Configuration

Create/open a new project
Make sure you have installed the needed project dependencies:
You need PyQt5, PyQt5-sip and pyqt5-tools

PyCharm - add missing dependencies

You need to perform next steps only if you don't have PyQt5, PyQt5-sip and pyqt5-tools packages

In Project Interpreter interface click the + and install pyqt5, pyqt5-sip, pyqt5-tools
./images/PyCharm_InstallMissingDependencies.png

PyCharm - Configure StartQtDesigner Tool

From File => Settings => Tools => External Tools click the + button
Create StartQtDesigner Tool with next settings:

Now you can start QtDesigner from PyCharm

PyCharm - Configure OpenWithQtDesigner Tool

From File => Settings => Tools => External Tools click the + button
Create OpenWithQtDesigner Tool with next settings:

Now you can open and edit a ui file with QtDesigner

PyCharm - Configure PyUIC

./images/PyCharm_PyUICTool.png
Program: Select python3 interpreter from your virtual environment:
Arguments: -m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
Working directory: $FileDir$

Reference

External tools @jetbrains.com

Build your Qt App

Build your Qt App

You can use your ui, converted to py class in your python app

			import sys

			from PyQt5.QtWidgets import (
				QApplication, QMainWindow
			)

			from helloWorld import Ui_MainWindow


			class Window(QMainWindow, Ui_MainWindow):
				def __init__(self, parent=None):
					super().__init__(parent)
					self.setupUi(self)


			if __name__ == "__main__":
				app = QApplication(sys.argv)
				win = Window()
				win.show()
				sys.exit(app.exec())
		

QtDesigner Hands on

QtDesigner Hands on

GUI templates

Template Form Type Widgets Base Class
Dialog with Buttons Bottom Dialog OK and Cancel buttons laid out horizontally on the bottom-right corner QDialog
Dialog with Buttons Right Dialog OK and Cancel buttons laid out vertically on the top-right corner QDialog
Dialog without Buttons Dialog No QDialog
Main Window Main Window A menu bar at the top and a status bar at the bottom QMainWindow
Widget Widget No QWidget

Qt Designer's Widget Box

The widget box provides a selection of standard Qt widgets, layouts, and other objects that can be used to create user interfaces on forms.
You can add objects to a form by dragging the appropriate items from the widget box onto the form, and dropping them in the required locations.
./images/designer-main-window.png

References && Self-Learning

References && Self-Learning

Official Docs

The Qt team maintains a thorough documentation, as well as user guides, tutorials, etc. for working wit QtDesigner

Getting to Know Qt Designer
A Quick Start to Qt Designer
The full manual: Qt Designer Manual

QtDesigner Layouts

Using Layouts in Qt Designer

Exercises

Exercises

Task1: My_First_GUI_App

Create a simple GUI like the one shown bellow.
.PyQT_HW_1_My_First_GUI.gif
Use the Qt Designer and save the file as my_first_GUI.ui
Convert the generated my_first_GUI.ui file to python code, using the pyuic5 tool
Create your Python app file: my_first_GUI_app.py which will start a Qt App and shows the GUI created by you with QtDesigner.