Why Qt for Python?¶
Screenshot from tiobe.com/tiobe-index/python, on 2021.09.06
To answer this question we need to take a step back, and talk a bit about languages.
Python has been around for almost the same amount of years that Qt has, and similarly it has been growing, and transforming to become the most used, loved, and demanded language for many programming areas.
Currently (2021), it’s rare to be aware of Machine Learning and Artificial Intelligence, without having heard of Python. Similarly, when we hear about Data Science/Analysis/Engineering we know that it is most probably related to Python.
One can validate this statements by public surveys that have been showing the evolution and preference of the Python language, like the StackOverflow Surveys of the lasts years:
Most Loved Language |
2nd place |
3rd place |
6th place |
Most Wanted Language |
1st place |
1st place |
1st place |
and the TIOBE index (image on the right).
It’s natural to think that this sources might not be enough to judge the language in general terms, but it certainly highlights a trend among developers around the world.
Lowering the Qt Barrier¶
Veteran C++ developers will have no problem with setting up a Qt application from scratch, or even manage to understand a different code base written with Qt. In addition, many teams are multidisciplinary, and other project/company developers might not be fluent in C++.
Python has been luring people into programming, and for the same reason it’s not uncommon that even people with a different background are able to write code, meaning that different teams are enabled to speak “the same language”.
Creating Qt applications in Python requires only a few lines of code, and not much configuration is required to execute it. As an /unfair/ example, let’s check the code of a simple hello world application:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
private slots:
void handleButton();
private:
QPushButton *m_button;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
m_button = new QPushButton("My Button", this);
connect(m_button, SIGNAL(clicked()), this,
SLOT(handleButton()));
}
void MainWindow::handleButton()
{
m_button->setText("Ready");
}
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return app.exec(d);
}
import sys
from pyside6.QtWidgets import (QApplication, QMainWindow,
QPushButton)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.button = QPushButton("My Button", self)
self.button.clicked.connect(self.handleButton)
def handleButton(self):
self.button.setText("Ready")
if __name__ == "__main__":
app = QApplication([])
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec())
It’s fair to say that most of the boilerplate code is provided by many good IDEs, like QtCreator, but using external tools certainly requires some practice to use them and get familiarized.
Unity Makes Strength¶
In our mission to enable more developers to enter the Qt World, it’s important to note that this doesn’t imply C++ developers are forgotten.
Together with the bindings, Qt for Python provides our binding generator, Shiboken (Check Binding Generation: What Is Shiboken?), whose functionality has extensibly been shown by talks on events such as those from our Videos section.
Generating bindings between two languages it nothing new, but it has always been a non-trivial task, mainly for being as-compatible-as-possible when using external modules/libraries in your project.
Shiboken’s main use case is to extend Qt/C++ project’s functionality, making them scriptable.
What does it mean for an application to be scriptable?
enables a interpreted language to interact directly with the Qt/C++ application,
provide the option to modify and create components/elements of the application from Python,
possibility to create a plugins/add-ons system for the application.
complement a process with external Python functionality.
Check out this Shiboken Webinar for a hands-on example.
Shiboken excels at Qt-dependent binding generation, meaning that any Qt/C++ project can be easily exposed to Python. In addition, Shiboken has proven its support for C++ projects (without Qt), as shown on event talks and blog posts.
Adding Python support to well known solutions/projects is a pattern we keep seeing in the industry, on a broad range of devices. This is why we are working every day to improve the Qt for Python offering.
We believe both Qt and Python will benefit from this interaction.