Hello Qt for Python

The first Qt for Python technology preview release is almost here, and for this reason we want to give a brief example on how it will open the doors to the Python world.

Let's build a simple application to show the simplicity of Qt for Python using QWidgets.
Every script will have more or less the same structure:

  1. We create a QApplication
  2. Then we include all the QWidgets and structures we want to use (e.g., a QLabel)
  3. We show the application and start our QApplication

If we put these ideas together, we will have something like this:

# hello_world.py
from PySide2.QtWidgets import QApplication, QLabel

app = QApplication([]) label = QLabel("Hello Qt for Python!") label.show() app.exec_()

To execute it, a simple python hello_world.py will do the work.

But that's not the whole story, the real question is: how to access the methods of a Qt class?
To simplify the process, we kept Qt APIs. For example, if we want to specify the size of a QLabel, in C++ we will have something like:

QLabel *label = new QLabel();
label->setText("Hello World!");
label->resize(800, 600);

The equivalent using Qt for Python will be:

label = QLabel()
label.setText("Hello World!")
label.resize(800, 600)

Now that we know the equivalent from C++ we can a write a more sophisticated application.

import sys
import random
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (QApplication, QWidget,
    QPushButton, QLabel, QVBoxLayout)

class MyWidget(QWidget): def __init__(self): QWidget.__init__(self)

self.hello = ["Hallo welt!", "Ciao mondo!", "Hei maailma!", "Hola mundo!", "Hei verden!"]

self.button = QPushButton("Click me!") self.text = QLabel("Hello World") self.text.setAlignment(Qt.AlignCenter)

self.layout = QVBoxLayout() self.layout.addWidget(self.text) self.layout.addWidget(self.button) self.setLayout(self.layout)

self.button.clicked.connect(self.magic)

def magic(self): self.text.setText(random.choice(self.hello))

if __name__ == "__main__": app = QApplication(sys.argv) widget = MyWidget() widget.resize(800, 600) widget.show() sys.exit(app.exec_())

If you are not familiar with Qt development, it is a common practice to extend a certain class and adapt it to our needs. In the previous example, we are using QWidget as a base class, and we included a QLabel and a QPushButton.

The application is really simple:

  1. First, we write a list containing Hello World in many languages.
  2. Then, we initialize a QPushButton and a QLabel with a certain alignment, font and size.
  3. After that, we create a QVBoxLayout to include our objects, and we assign it to our class.
  4. And finally, we connect the clicked() signal of our QPushButton to our method called magic.

As a result, every time we click the button, we will get a Hello World in a random language!

The structure of this simple script will be the base for most of the applications using Qt for Python, and we encourage you to test it as soon as Qt for Python is out!


Blog Topics:

Comments