This series of articles is the work of one very creative and intelligent person with the nickname
Liksys . Thanks to good people, he received an invite and the continuation of this series of articles will certainly be! ;)

Part number 1
When people talk about Qt, many of us immediately recall KDE with its abundance of settings. Programmers follow the source code written in C ++ with a very elegant approach to solving the problem. And this is quite justified: KDE is perhaps the most famous Qt project, and Qt itself is written in C ++ and is intended primarily for it. For a long time, the existence of this toolkit has become a kind of standard in Linux for writing graphical applications along with GTK, wxWidgets, etc. The power of C ++ has made it possible to create many great programs, and the Qt style is a role model for many programmers.
But not everything is so simple. The irony lies in the fact that the tricked out and functionality of C ++ for final programs is very often redundant. For writing a small application, you have to perform a lot of actions, and the use of pointers and other “frills” C ++ in the code, along with Qt tools, does not look very good. After all, the program is not always comparable in complexity to the same KDE, which uses all the features of Qt (by the way, there are few of them, and KDE’s have written many extensions). Do not forget even the time that is spent on compiling the program - because of this, the productivity of work is greatly reduced.
However, in the world of OpenSource there is always an alternative, and here it is also there. This is Python. Python, unlike C ++, is much simpler to learn, has an intuitive and logical syntax, and its dynamic typing, automatic memory management, and a rich set of built-in high-level types make it possible to write highly efficient and compact code. The combination of Python and Qt4 allows you to quickly create convenient applications.
with graphical interface.
Another undoubted advantage of Python is its implementation. You might think that Python is an interpretable language, but that’s not quite true. When the program starts, the files ".pyc" are created
the modules used are Python compiled bytecode. Compiling into a buy-code occurs automatically (if possible) and takes very little time, unlike compiling into executable files with C ++ - you can replace a couple of lines and immediately start the program - no need to wait until the program has finished building, everything happens completely unnoticed. It is very convenient
for the programmer.
And finally, the question of speed. Python is quite fast, so the user doesn’t even notice the difference, and if you still want to speed up the program, you can use the Psyco module - it includes JIT compilation, and the performance of a program that performs many calculations immediately increases.
If some of these terms are incomprehensible to you - do not be upset, as I read, I will explain everything to you. So let's get started.
We will use Qt4, because it will displace Qt3 soon, and it has many innovations compared to it, and writing programs is much more pleasant.
Qt4 implementation for Python is called PyQt4 (logical, isn't it?) And is produced by Riverbank. It is distributed in the same way as Qt4: under a proprietary license and under the GPL. Download the latest version can usually be from the repository of your distribution, but if it was not there, then it can be done on the website
www.riverbankcomputing.co.uk . There are implementations of PyQt4 not only for Linux, but also for MacOSX, Windows. I, for example, often
I am writing Python programs for Windows (although I don’t like it, but nobody cares). Programs written in Python and Qt4 are readable - they can be easily run on another platform.
even without recompilation!
PyQt4 is a “wrapper” to the Qt4 library, made using the SIP toolkit for creating bindings to libraries. Therefore, Qt4 itself is required for PyQt4 to work. It can also be installed from the repository or from the Trolltech website:
www.trolltech.com .
Installing PyQt4 should not be a problem, even if you are installing from source. However, I would still recommend searching your repositories for your distribution.
After installation, you can immediately start creating programs. I think that I have already provoked you enough, so let's write a couple of simple programs - one in C ++, and the second in Python, using Qt4. At the same time, we compare the ease of writing so that my statements do not look unfounded.
Let's start with C ++. We will work in the console, so you, as a real jigit, need to be able to curb it. Typical actions are to create a project directory, create source files,
Create a Qt project file, create a Makefile from it (a script file for GNU make) and start the build. Then, if your prayers to heaven have been heard, you will receive a ready-made executable file named like the directory of your project. The description looks scary (I'm not talking about how to do all this with the files created by autotools, but this is irrelevant). So, open the console.
')
[liksys @ max ~] $ mkdir helloqt
[liksys @ max ~] $ cd helloqt
[liksys @ max helloqt] $ cat> main.cpp
#include <QApplication>
#include <QLabel>
int main (int argc, char ** argv)
{
QApplication app (argc, argv);
QLabel * label = new QLabel ("Hello, Qt4!");
label-> show ();
return app.exec ();
}
<Ctrl + D>
[liksys @ max helloqt] $ qmake-qt4 -project
[liksys @ max helloqt] $ qmake-qt4
[liksys @ max helloqt] $ make
-There are the compiler messages, I hope everything went well,
[liksys @ max helloqt] $ ./helloqt
Here is our first Qt4 application. If you received messages in the spirit of:
main.cpp: 1: 24: error: QApplication: There is no such file or directory
main.cpp: 2: 18: error: QLabel: There is no such file or directory
main.cpp: In function 'int main (int, char **)':
main.cpp: 5: error: no 'QApplication' declaration in this scope
...
then you used Qt3 utilities, not Qt4. So clarify the paths to them and try again and use qmake-qt4 than just qmake (it most likely belongs to Qt3).
The process is a bit scary, but now calm down and write the same thing in Python. We need to know only one thing - the path to the Python interpreter, this can be done using the which command. Going to
home directory, do the following:
[liksys @ max helloqt] $ which python
/ usr / bin / python
[liksys @ max ~] $ mkdir hellopyqt
[liksys @ max ~] $ cd hellopyqt
[liksys @ max hellopyqt] $ cat> hellopyqt
#! / usr / bin / python
from PyQt4 import Qt
import sys
if __name__ == "__main__":
app = Qt.QApplication (sys.argv)
label = Qt.QLabel ("Hello, PyQt4!")
label.show ()
app.exec_ ()
[liksys @ max hellopyqt] $ chmod + x hellopyqt
[liksys @ max hellopyqt] $ ./hellopyqt
As you can see, in the case of Python, everything is much simpler. Just find out the path to the interpreter, create a file with a Python program and make it executable. Everything! Now you can run the program like any other.
Let's take a closer look at everything that we have done, and begin, perhaps, from the console.
First we learned the path to the interpreter. For what? Well, if you are an ac console and are familiar with writing shell scripts, you will immediately understand what the matter is. For those who do not know, I will explain. The system does not know how
run the file for execution. In UNIX, there is no binding to the file extension; instead, “Magic Numbers” are used that uniquely identify the file type. Linux executable binaries have at the very beginning a certain magic number, according to which the system finds out what kind of file it is. Then how to deal with scripts (another term that would be nice to remember, is this any interpreted file, often for example you can hear shell scripts)? For this was provided a special sequence of characters - "#!", Also called "shibeng". After this sequence of characters indicate the path to the program that executes the script. In this case, it is Python. The program can also be launched and directly calling the interpreter (while your program is desirable, it should have the .py extension, at least for readability):
[liksys @ max ~] $ python myprog.py
Next, we created a directory for the program. Generally speaking, for such a simple program like “Hello”, it was not necessary to do this, but for large projects it is simply necessary. Python looks for modules other than system (and user-defined) paths in the current directory, so it would be nice to limit it somehow.
After creating the file, we made it executable and launched. These are standard actions, and if you have previously written shell scripts, you will recognize this sequence of commands.
Let's take a closer look at the program itself. The difference with the analogue in C ++ is immediately visible - visually the program looks simpler and takes up a little less space.
About the first line has already been said - we set the path to the interpreter. The next two lines import the necessary modules - this is PyQt4 and sys. The sys module is imported completely, but from PyQt4 only part is imported - Qt. QtGui, QtCore, etc. modules are automatically imported into it. Of course, you can import each part of PyQt4 separately, but you can do it easier, thus creating fewer problems for yourself (it’s not necessary to remember which module contains the required class). By importing Qt, we also get rid of the compatibility problem - our own classes will not conflict with the library ones. The fourth line of the program may not be entirely clear to those who have recently been in Python. The Python module can be executed by itself (for example, when debugging), so we include the code by the condition: if the file is executed, then we do certain actions. If this file is simply imported, then these actions will not be performed. For this magic is responsible variable __name__, which contains the name of the function. In our “Hello” this was not necessary, but a good style requires the decoration of the main () function (as in many other languages, for example, in C ++).
Then comes the code responsible for the interface. First, an instance object QApplication is created that manages all the resources of the application. Qt itself handles some command line options, so as a parameter, we pass to the sys.argv constructor - the list of options with which the program was launched.
The next line creates a label widget — an instance object of the QLabel class. This is a text label containing an inscription. A widget is another term that you will often encounter. is he
denotes any user interface element, such as a text label or button. Some widgets can contain other widgets - for example, the QMainWindow, which contains the other controls, is often the main window. But no one forces to use QMainWindow, which we did in the previous example - Qt allows you to use any widget as a window.
If you know HTML (which is generally desirable when working with Qt), then you can have a little fun and replace the label text in the QLabel constructor with "
Hello, PyQt4 ! ". Almost all Qt widgets that work with text in some way or other support simplest HTML formatting tools.
The next short line - “label.show ()”, makes our widget visible. In Qt, all default widgets are created invisible to allow their settings - and then they are displayed on the screen. This avoids flicker and other unpleasant effects. In a large application, you only need to make one main widget visible, and all the widgets it contains will become visible automatically.
And the last line is the transfer of control to the application6, namely the QApplication instance object. At this point, the program enters the event loop, waiting for action from the user. However, we see a slight difference - in C ++ the function was called exec (), and in Python it was called exec_ (). Why is this so? In Python, the exec keyword exists, so the PyQt4 developers have adopted a simple rule: if the name of the original Qt4 function matches the Python keyword, then the function name is followed by the symbol "_". This rule applies to all PyQt4 functions.
As you can see, the Python program is easier. No header files for each class separately, no pointers, and other C ++ tricks. Of course, you have to get a little used to the different wisdom of Python, but one thing is clear - for programs with a graphical interface, Python fits very well. You can create programs faster and shorter using the full power of Qt4 for C ++ and the convenience of Python. Not for nothing, this approach is popular with many developers, both OpenSource and commercial companies, the benefit of Python is distributed on a large number of licenses for every taste, to pay commercial companies that produce products with closed source texts, only for Qt4 and PyQt4. But we are just learning, right? Therefore, we can fully use these libraries under the terms of the GPL license, releasing all our creations in clear text. And closed software is a business of commercial companies that can afford it.
I hope I managed to interest you, because then it will be even more interesting! We will learn how to create our own applications on PyQt4, our own widgets, I will tell you how to properly locate modules, use modern technologies such as internationalization. We will talk about how to organize an application.
in terms of ergonomics and design, because the interface is the most important part of the product, it is the user who sees it in front of him. As we study, we will write a lot of simple applications that illustrate this or that concept, and in the end we will try to write something interesting using all the knowledge.

Wait part number 2. I hope you have learned a lot, thank you for your attention;)