Stay up to date with the latest marketing, sales and service tips and news.
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:
If we put these ideas together, we will have something like this:
# hello_world.py
from PySide2.QtWidgets import QApplication, QLabelapp = 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:
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!
Stay up to date with the latest marketing, sales and service tips and news.
Download the latest release here: www.qt.io/download.
Qt 5.12 was developed with a strong focus on quality and is a long-term-supported (LTS) release that will be supported for 3 years.
Find webinars, use cases, tutorials, videos & more at resources.qt.io
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.Henkilön Qt (@theqtcompany) jakama julkaisu