Porting a C++ Application to Python — Qt for Python (original) (raw)

Qt for Python lets you use Qt APIs in a Python application. So the next question is: What does it take to port an existing C++ application? Try porting a Qt C++ application to Python to understand this.

Before you start, ensure that all the prerequisites for Qt for Python are met. SeeGetting Started for more information. In addition, familiarize yourself with the basic differences between Qt in C++ and in Python.

Basic differences

This section highlights some of the basic differences between C++ and Python, and how Qt differs between these two contexts.

C++ vs Python

var = None def func(key, value = None): """Does stuff with a key and an optional value.

If value is omitted or None, the value from func()'s last call is reused. """ global var if value is None: if var is None: raise ValueError("Must pass a value on first call", key, value) value = var else: var = value doStuff(key, value)

In this example, func() would treat var as a local name without the global statement. This would lead to a NameError in the value is None handling, on accessing var. For more information about this, seePython refernce documentation.

Tip

Python being an interpreted language, most often the easiest way is to try your idea in the interperter. You could call the help() function in the interpreter on any built-in function or keyword in Python. For example, a call to help(import) should provide documentation about the import statment

Last but not the least, try out a few examples to familiarize yourself with the Python coding style and follow the guidelines outlined in thePEP8 - Style Guide.

import sys

from PySide2.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv) label = QLabel("Hello World") label.show() sys.exit(app.exec_())

Note

Qt provides classes that are meant to manage the application-specific requirements depending on whether the application is console-only (QCoreApplication), GUI with QtWidgets (QApplication), or GUI without QtWidgets (QGuiApplication). These classes load necessary plugins, such as the GUI libraries required by an application. In this case, it is QApplication that is initialized first as the application has a GUI with QtWidgets.

Qt in the C++ and Python context

Qt behaves the same irrespective of whether it is used in a C++ or a Python application. Considering that C++ and Python use different language semantics, some differences between the two variants of Qt are inevitable. Here are a few important ones that you must be aware of:

Here is the improved version of the Hello World example, demonstrating some of these differences:

1 2import sys 3import random 4 5from PySide2.QtWidgets import (QApplication, QLabel, 6 QPushButton, QVBoxLayout, QWidget) 7from PySide2.QtCore import Qt, Slot 8 9class MyWidget(QWidget): 10 def init(self): 11 super().init() 12 13 self.hello = ["Hallo Welt", "Hei maailma", "Hola Mundo", "Привет мир"] 14 15 self.button = QPushButton("Click me!") 16 self.text = QLabel("Hello World") 17 self.text.setAlignment(Qt.AlignCenter) 18 19 self.layout = QVBoxLayout() 20 self.layout.addWidget(self.text) 21 self.layout.addWidget(self.button) 22 self.setLayout(self.layout) 23 24 self.button.clicked.connect(self.magic) 25 26 @Slot() 27 def magic(self): 28 self.text.setText(random.choice(self.hello)) 29 30if name == "main": 31 app = QApplication(sys.argv) 32 33 widget = MyWidget() 34 widget.resize(800, 600) 35 widget.show() 36 37 sys.exit(app.exec_())

Note

The if block is just a good practice when developing a Python application. It lets the Python file behave differently depending on whether it is imported as a module in another file or run directly. The__name__ variable will have different values in these two scenarios. It is __main__ when the file is run directly, and the module’s file name (hello_world_ex in this case) when imported as a module. In the later case, everything defined in the module except the if block is available to the importing file.

Notice that the QPushButton’s clicked signal is connected to the magic function to randomly change the QLabel’s text property. The @Slot` decorator marks the methods that are slots and informs the QtMetaObject about them.

Porting a Qt C++ example

Qt offers several C++ examples to showcase its features and help beginners learn. You can try porting one of these C++ examples to Python. Thebooks SQL exampleis a good starting point as it does not require you to write UI-specific code in Python, but can use its .ui file instead.

The following chapters guides you through the porting process:

© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.