aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/doc/faq/whyqtforpython.rst
blob: 0f0ab9aaf2581a0a9a2e28b7822afcb6a69f9a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
.. _whyqtforpython:

Why Qt for Python?
==================

.. image:: tiobe.png
   :width: 0

.. raw:: html

    <div style="float: right; padding-left: 20px; max-width: 30%;
    background-color: #e9eff5; padding-top: 5px;">
      <img src="../_images/tiobe.png"
          style="width: 90%;"
          alt="TIOBE index for Python" />
      <p style="font-size: 80%;">
      Screenshot from
      <a href="https://www.tiobe.com/tiobe-index/python/">tiobe.com/tiobe-index/python</a>,
      on 2021.09.06
      </p>
    </div>

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:

+----------------------+-----------+-----------+-----------+
|                      | 2019_     | 2020_     | 2021_     |
+======================+===========+===========+===========+
| 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:

.. tab-set::

   .. tab-item:: C++ Header

       .. code-block:: cpp

          #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

   .. tab-item::  C++ Implementation

       .. code-block:: cpp

          #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");
          }

   .. tab-item:: C++ Main

       .. code-block:: cpp

          #include <QApplication>
          #include "mainwindow.h"

          int main(int argc, char *argv[])
          {
              QApplication app(argc, argv);
              MainWindow mainWindow;
              mainWindow.show();
              return app.exec(d);
          }

.. tab-set::

   .. tab-item:: Python

      .. code-block:: python

         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 :ref:`whatisshiboken`), whose functionality has
extensibly been shown by talks on events such as those from our
:ref:`video-gallery` 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.

.. _2019: https://insights.stackoverflow.com/survey/2019
.. _2020: https://insights.stackoverflow.com/survey/2020
.. _2021: https://insights.stackoverflow.com/survey/2021
.. _`TIOBE index`: https://www.tiobe.com/tiobe-index/
.. _`blog posts`: https://www.qt.io/blog/tag/qt-for-python
.. _`Shiboken Webinar`: https://www.youtube.com/watch?v=wOMlDutOWXI