Getting to know... Qt for Python

I recently took a look at Dash which seems great for data visualization, but I have a project for which I'd love to build a user interface. I previously used PySimpleGUI and the result was to my satisfaction. However, there was something left to be desired. In any case, that brings me to Qt (pronounced "cute") for Python. Let's check out the documentation and see what we can do with it.

Headings in this post follow the headings in the documentation.

Qt for Python Quick start

`pip install PySide2`

Right off the bat. The `` seems more difficult to create something simple (versus PySimpleGUI). However, I imagine all this complexity is what eventually enables a more advanced user interface (I later learn about QtQuick/QML).

Remark: I encountered an error for which I tried various environment variable fixes. Eventually the solution which worked for me was this answer from stackoverflow. But I wasn't happy with just implementing the solution. What I learned was I had incorrectly set the necessary environment variable. To correctly set the environment variable, run the following in the Python terminal:
`import os`
`import PySide2`
`dirname = os.path.dirname(PySide2.__file__)`
`plugin_path = os.path.join(dirname, 'plugins', 'platforms')`
Create a new environment variable named `QT_QPA_PLATFORM_PLUGIN_PATH` and point it to the above path.

Qt for Python Getting Started

This section was about "building Qt for Python from source." I was not interested in this.

Qt for Python Modules

This section serves as a table of contents to all the modules. There are five major categories: "Basic Modules," "QML (Qt Modeling Language) and Qt Quick," "Data visualization," "Multimedia," and "WebEngine."

Qt for Python Tutorials

Basic Tutorials

Your First QtWidgets Application

This is a simpler "hello world" application than the one shown previously in "Qt for Python Getting Started."

I noted that "`QLabel` [...] can present text (simple or rich, like html) and images."

Your First Application Using PySide2 and QtQuick/QML

This example shows that an application can be designed using QML, a declarative language. A basic application consists of a QML file and a python file which loads the QML file.

The page makes a note about programming for desktop.

A Simple Button Tutorial

I read through the page and then I copied the complete code at the end into a file called ``. Run it using `python`.

The page said not to worry about what a slot is, but I felt compelled to look into it. The high-level idea seems to be that objects can have inputs called slots and outputs called signals. In the design of an application, one can connect signals to slots.

Creating a Simple PySide2 Dialog Application

I read through the page and then I copied the complete code at the end into a file called ``. Run it using `python`.

Using UI Files

Interesting. Apparently one can use a program called Qt Creator to form the user interface file, the page gives an example code for a file named `mainwindow.ui`. This file can then either be imported as a class (after converting it with `pyside2-uic`) or loaded directly (with `QUiLoader` from `PySide2.QtUiTools`). The final example code uses the latter method (loading the UI file directly) and is called ``. However, I named it `` after the documentation page.

Fix: the code uses the library QIODevice but forgot to import it. So I just added the import (e.g., `from PySide2.QtCore import QFile, QIODevice`).

Real use-cases applications

Data Visualization Tool Tutorial

Chapter 1 - Reading data from a CSV

The first example uses pandas to read a CSV. The file name is read in as an argument in the command line.

Because the documentation does not provide data, I generated my own dummy data (see `Generate Test Data.ipynb`).

Chapter 2 - Filter data

Aside from typical pandas actions like filtering data, this next example implies Qt has its own datetime object, `QDateTime`. The example reads the data, filters it, and extracts two columns from it. One of the two columns, is converted from a column of UTC (datetime) strings to one of `QDateTime` objects.

Initially the dummy data I generated was not compatible with the UTC string format in the example code. Rather than tweak the example code, I decided to tweak my dummy data.

Chapter 3 - Create an empty `QMainWindow`

Interestingly, this section provides example code which does not generate any output. Fortunately, it was simple enough to refer to the "Using UI Files" section and add the following snippet to the end of the example code:
if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = MainWindow()


Remark: the code also refers to a module called `qApp` from `QtCore` but that throws an error. I was able to fix the error by replacing this import with`QApplication` from QtWidgets and replacing all references to `qApp` with `qApplication`.

The reader may find the full file in my Github repo as ``.

Chapter 4 - Add a QTableView

This section ramps up the difficulty a little but I find it more useful to focus on the big picture. The section has the following files with the following classes and dependencies of other files.

1) `` defines `CustomTableModel`
2) `` defines `Widget` and imports `CustomTableModel` from `table_model`
3) `` defines `MainWindow`
4) `` is the main app and imports `MainWindow` from `main_window` and `Widget` from `main_widget`

The file `` is a small variation of `` in the previous chapter. The difference is that `MainWindow` now gets passed a `widget` which it sets as the central widget via `setCentralWidget()`. Meanwhile, the file `` is essentially `` from Chapter 2 but instead of printing data to the terminal, passes the data to form a `Widget`. The `widget` instance is then passed to form a `MainWindow` and this is shown to the user.

Run the app using the provided test data by typing `python -f test_data.csv`.

Observe that the original code results in the left column to adjust to the contents and the remaining space to be allocated to the second column. Rather than focus on fixing this sight for sore eyes, I decided to leave it be and simply acknowledge that there are various settings for adjusting the column width. In particular, while the example used `setSectionResizeMode()` and `setStretchLastSection()`, one can explicitly set a column width with `setColumnWidth()`

Chapter 5 -

Similar to the previous except with `` defining a `Widget` class which contains a `QChartView`. In the Github repo, I placed the example code in a file called ``. I then copied `` from the previous chapter to `` and changed the import.

Run the app using the provided test data by typing `python -f test_data.csv`.

Chapter 6 -

Finally, this final step adds a method to the `` file of the previous chapter which plots the series. I named this new file `` and made a copy of the file `` as `` to import from it.*

Run the app using the provided test data by typing `python -f test_data.csv`.

*After doing a test run, it occurred to me that the program assumes the data is sorted so I added a line in the `read_data` method which sorts the incoming data by the "time" column.

Other Examples

There are other examples but I decided to end here for now.

C++ and Python

I skip this section.

Other sections

There are some more sections but I decided to end here for now.

Relevant Links:
Getting to Know Qt for Python (Github Repo)


Popular posts from this blog

Getting to know... D3

PySpark + Anaconda + Jupyter (Windows)