Learning PyQt

One of my recent undertakings has been, to extend my knowledge of Python, which I was previously only capable of writing procedural code for, to include, how to write Object-Oriented Python.

In the process, I began to think of what advantages I might now have, with that ability. And one answer which presented itself was of the form, ‘I already know enough about the Qt Library, to use it for some C++ programs. It has a Python binding referred to sometimes as PyQt. With the ability to write Object-Oriented Python, I should also gain the ability to write GUI applications in Python – eventually.’

The result of my recent exercise can be found at this URL:

https://dirkmittler.homeip.net/binaries/

The compressed files which contain my first project using PyQt are named ‘PyQt_Test_1_s.gz‘ and ‘PyQt_Test_1_s.zip‘. Either of those compressed archives need to be unzipped to a folder, in which there should be a total of 4 Python scripts. Python 3 would need to be run on the script named ‘AppStart.py‘.

I’m sorry to start so small.

Oh, yes… In order for these scripts to run, the reader’s Python installation would need to include PyQt5. Not all do.

Dirk

Getting FreeFem++ to display impressive visuals under Linux.

One of my Computing habits is, to acquire many frameworks, for performing Scientific or Analytical Computing, even though, in all honesty, I have little practical use for them, most of the time. They are usually of some Academic curiosity to me.

Some of the examples familiar to me are, ‘wxMaxima‘ (which can also be installed under Linux, directly from the package manager), ‘Euler Math Toolbox‘ (which, under Linux, is best run using Wine), and ‘SageMath‘ (which IMHO, is best installed under Linux, as a lengthy collection of packages, from the standard repositories, using the package manager, that include certain ‘Jupyter’ packages). In addition to that, I’d say that ‘Python‘ can also be a bit of a numerical toolbox, beyond what most programming languages can be, such as C++, yet, a programming language primarily, which under Linux, is best installed as a lengthy collection of packages through the package manager. And a single important reason is the fact that a Python script can perform arbitrary-precision integer arithmetic natively, and, with a small package named ‘python3-gmpy2′, can also perform arbitrary-precision floating-point arithmetic easily. If a Linux user wanted to do the same, using C, he or she would need to learn the library ‘GMP’ first, and that’s not an easy library to use. Also, there exists IPython, although I don’t know how to use that well. AFAICT, this consists mainly of an alternative shell, for interacting with Python, which makes it available through the Web-interface called “Jupyter”. Under Debian Linux, it is best installed as the packages ‘ipython3′, ‘python3-ipython-genutils’, ‘python3-ipykernel’, ‘python3-nbconvert’, and ‘python3-notebook’, although simply installing those packages, does not provide a truly complete installation… Just as one would want a ‘regular’ Python installation to have many additional packages, one would want ‘IPython’ to benefit from many additional packages as well.

But then, that previous paragraph also touches on an important issue. Each Scientific Computing platform I learn, represents yet-another scripting language I’d need to learn, and if I had to learn 50 scripting languages, ultimately, my brain capacity would become diluted, so that I’d master none of them. So, too much of a good thing can actually become a bad thing.

As a counter-balance to that, it can attract me to a given Scientific Computing platform, if it can be made to output good graphics. And, another Math platform which can, is called “FreeFem“. What is it? It’s a platform for solving Partial Differential Equations. Those equations need to be distinguished from simple derivatives, in that they are generally equations, in which a derivative of a variable is being stated on one side (the “left, bilinear side”), but in which a non-derivative function of the same variable is being stated on the other (the “right side”). What this does, is to make the equation a kind of recursive problem, the complexity of which really exceeds that of simple integrals. (:2)  Partial Differential Equations, or ‘PDE’s, are to multi-variable Calculus, as Ordinary Differential Equations, or ‘ODE’s, are to single-variable Calculus. Their being “partial” derives from their derivatives being “partial derivatives”.

In truth, Calculus at any level should first be studied at a University, before computers should be used as a simplified way of solving its equations.

FreeFem is a computing package, that solves PDEs using the “Finite Element Method”. This is a fancy way of saying, that the software foregoes finding an exact analytical solution-set, instead providing an approximation, in return for which, it will guarantee some sort of solution, in situations, where an exact, analytical solution-set could not even be found. There are several good applications. (:1)

But I just found myself following a false idea tonight. In search of getting FreeFem to output its results graphically, instead of just running in text mode, I next wasted a lot of my time, custom-compiling FreeFem, with linkage to my many libraries. In truth, such custom-compilation is only useful under Linux, if the results are also going to be installed to the root file-system, where the libraries of the custom-compile are also going to be linked to at run-time. Otherwise, a lot of similar custom-compiled software simply won’t run.

What needs to be understood about FreeFem++ – the executable and not the libraries – is, that it’s a compiler. It’s not an application with a GUI, from which features could be explored and evoked. And this means that a script, which FreeFem can execute, is written much like a C++ program, except that it has no ‘main()‘ function, and isn’t entirely procedural in its semantics.

And, all that a FreeFem++ script needs, to produce a good 2D plot, is the use of the ‘plot()‘ function! The example below shows what I mean:

I was able to use an IDE, which I’d normally use to write my C++ programs, and which is named “Geany”, to produce this – admittedly, plagiarized – visual. The only thing I needed to change in my GUI was, the command that should be used, to execute the program, without compiling it first. I simply changed that command to ‘FreeFem++ "./%f"‘.

Of course, if the reader wants in-depth documentation on how to use this – additional – scripting language, then This would be a good link to find that at, provided by the developers of FreeFem themselves. Such in-depth information will be needed, before FreeFem will solve any PDEs which may come up within the course of the reader’s life.

But, what is not really needed would be, to compile FreeFem with support for many back-ends, or to display itself as a GUI-based application. In fact, the standard Debian version was compiled by its package maintainers, to have as few dependencies as possible (‘X11′), and thus, only to offer a minimal back-end.

(Updated 7/14/2021, 21h45… )

This time, I used a QPainter object in a very ordinary way, as would be done in genuine GUI-building.

One project which I have been posting about has been, my ongoing effort to teach myself how to use the Qt5 GUI Library, which professional programmers do use in order to design Graphical User Interfaces – GUIs – for applications / programs. In fact, in a preceding posting, I wrote that I had discovered a deliberately unusual way to use one of the object classes in Qt 5.7.1, so that a ‘QImage’ object could be rendered to the screen of a computer, without first having to be transformed into a ‘QPixmap’ object. The ordinary thing to do would be, to transform it thus, first.

That trick can increase performance if two situations are met:

• The graphical information could have started out as a bitmapped image, And
• That image could have been unusually large, so that to have to buffer the entire image in its uncompressed form several times, could pose a considerable drain on the amount of memory that even modern computers should part with.

But, when designing GUIs, the starting point of the graphical information can be, simulated brush-strokes, with a simulated ‘QBrush’ and ‘QPen’, that are orchestrated by a programmer to give logical meaning to his GUI (beyond just adding the predefined widgets to it). In that case, the trick which I just wrote about will not gain anything over what I am about to describe.

When the ‘QPainter’ object and session are initialized, their target can be a ‘QPixmap’ object, and, after a sequence of painting instructions has finished, it can be assigned to a ‘QLabel’ widget once, by way of the ‘.setPixmap()’ function, thus resulting in a custom widget. Much buffering takes place, and, if the code has been written correctly, the graphic appears on the screen. The following is the graphic which my latest exercise generated:

Admittedly, this does not correspond to the GUI of any real software. But hypothetically, it just as well might. In fact, ‘A Chess-Board’ is just as easy to construct, using the same methodology. Only, actually programming the computer to play Chess is a little harder.

The fact that I was able to post this graphic was partially due, to the added ability the Qt5 Libraries have, to Save a ‘QPixmap’ object, or a ‘QImage’ object, directly to the hard drive, using a simple, straightforward API, and to the format of a PNG File. That task is also achieved in the code sample I am about to link to.

The real caveat here is, Not to try doing funny stuff with the internal ‘.pixmap()’ pointer that belongs to the ‘QLabel’ object, because even though it can be done, writing to that internal property does not offer much in the way of performance improvements, when the goal is to design most GUIs.

(This is a link to the previous exercise.)

(Updated 8/21/2020, 21h25… )

Basic Qt5 Slots, Signals and Menus Example

In case the reader is an aspiring programmer, but yet a hobbyist, one of the facts which he or she will already know is that, in order to design applications which have a Graphical User Interface, some predefined GUI Library is commonly used, and one example of a GUI Library which can be used is Qt 5. Therefore, in order to become proficient at using this library, like me, the reader might want a working example of:

• Signals and Slots – The way in which Qt connects user-actions on the GUI, to actions which should be triggered from the actual program,
• The use of the ‘QLabel’ class to display an image instead of more-common text,
• The design of a very basic command-menu and a keyboard shortcut,
• A QRC Resource Script.

Even though this example was created with ‘Qt Creator’ and Qt version 5.7, one of the main features of Qt Creator, its GUI Layout Designer, has been cut from the project, so that the means by which such mechanisms can be set up entirely via code, can be explored better. Also, while Qt5 maintains backwards-compatibility with Qt4 -style Signals and Slots, that are based on macros, this project makes use of the newer Qt5 semantics, that are based on function pointers, for the sake of favouring new features over old.

I can say that on my Debian 9 / Stretch computer, the example works. However, the Qt Library is designed to be cross-platform, and so the example should also work under Windows. What some people have suggested is that, in order to get such code to work under OS/X, ‘ccmake’ should be used with the ‘Unix Makefiles’ generator. This will assume that ‘XCode’ is already installed. (:1)

The Link where the compressed files, containing only source code, can be found (along some other compressed files that also contain precompiled binaries, belonging to other projects) is here:

https://dirkmittler.homeip.net/binaries/

In that Gopher-Hole, the files of interest would be ‘Creator_Test2.tar.gz‘ or ‘Creator_Test2.zip‘.