Hello Qt for Python
May 04, 2018 by Cristián | Comments
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:
- We create a QApplication
- Then we include all the QWidgets and structures we want to use (e.g., a QLabel)
- 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)
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:
- First, we write a list containing Hello World in many languages.
- Then, we initialize a QPushButton and a QLabel with a certain alignment, font and size.
- After that, we create a QVBoxLayout to include our objects, and we assign it to our class.
- 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!
Subscribe to our newsletter
Try Qt 6.5 Now!
Download the latest release here: www.qt.io/download.
Qt 6.5 is the latest Long-Term-Support release with all you need for C++ cross-platform app development.
Explore Qt World
Check our Qt demos and case studies in the virtual Qt World
Check out all our open positions here and follow us on Instagram to see what it's like to be #QtPeople.
Näytä tämä julkaisu Instagramissa.
Want to build something for tomorrow, join #QtPeople today! We have loads of cool jobs you don't want to miss! http://qt.io/careers #builtwithQt #software #developers #coding #framework #tool #tooling #C++ #QML #engineers #sales #tech #technology #UI #UX #CX #Qt #Qtdev #global #openpositions #careers #job
Henkilön Qt (@theqtcompany) jakama julkaisu