Qt for Python

PySide2

Cristián Maureira-Fredes
http://pyside.org

What is Qt?

  • Cross-platform application framework (C++)
  • ...but it is much more than a simple library

Scope

Besides UIs, with Qt we can:

  • Create command-line tools
  • Use features to develop others programs
    • SQL, XML, JSON, OpenGL
    • Thread Management, Networking, etc
  • Interaction between Objects

Signals & slots

System for communication between Objects

  • Simple observer pattern,
  • Avoid the boilerplate for:
    • Async I/O (pipes, serial devices, sockets)
    • Event notifications (timeouts)

Rapid application development

QtQuick
  • QML, a declarative scripting language
  • Focused on mobile devices
  • Can be mixed with native code.

Additional Tools

QtCreator

Additional Tools

QBS

  • Building tool
    • Android SDK and NDK, C/C++, Java, NSIS, TypeScript, WiX, XCode, etc.
    • Android, FreeBSD, iOS, Linux, macOS, QNX, tvOS, watchOS, Windows, etc

Solutions with Qt

  • Embedded Devices
  • UI and Software
  • Internet of Things
  • Mobile

Now imagine all that, but with Python

What is PySide2?

  • Set of modules for Python to use Qt
  • Allows to use Qt without the trouble of compiling
  • Compatible with Python 2 and 3

Origin

  • 2008: Started out as PySide for Qt4
  • 2009: Released by Nokia
  • Ice Age
  • 2015: Effort to port it to Qt5 on Github

Currently

  • 2016: Developed by "The Qt Company"
  • Goal become a first class citizen in Qt5

How does PySide2 look like?

Hello World


import sys
from PySide2.QtWidgets import QApplication, QLabel

if __name__ == "__main__":
  app = QApplication(sys.argv)

  label = QLabel("Hello World!")
  label.show()

  sys.exit(app.exec_())
    

Modules

  • Qt Core
    • Core non-graphical classes used by other modules
  • Qt GUI
    • Base classes for GUI components
  • Qt Widgets
    • Classes to extend Qt GUI with C++ Widgets
  • Qt Multimedia, Qt Network, Qt Quick, Qt Test, ...

How does it work?

  • Bindings generated by Shiboken
  • Uses CLang to parse Qt's headers (C++)
  • Flexible XML specification (Typesystem)

Under the hood

Shiboken

  • Generator plugin that outputs C++ code for CPython extensions
  • Why reinventing the wheel? Size
  • Non-Qt libraries can be wrapped

Typesystems


<modify-function signature="overloadedMethod(int, char**)">
  <modify-argument index="1">
    <replace-type modified-type="PySequence" />
  </modify-argument>
  <modify-argument index="2">
    <remove-argument />
  </modify-argument>
  <inject-code class="target" position="beginning">
    int argc;
    char** argv;
    if (!PySequence_to_argc_argv(%PYARG_1, &argc, &argv)) {
      PyErr_SetString(PyExc_TypeError, "error");
      return 0;
    }
    %RETURN_TYPE foo = %CPPSELF.%FUNCTION_NAME(argc, argv);
    %0 = %CONVERTTOPYTHON[%RETURN_TYPE](foo);

    for (int i = 0; i < argc; ++i)
      delete[] argv[i];
    delete[] argv;
  </inject-code>
</modify-function>

Examples

external link

Why PySide2?

  • The simplicity and robustness of Python
  • Easily connect Qt with Python modules
  • Cross-platform compatibility
  • Fast prototyping from Python to C++
  • Extend applications with scripts

Status

  • Support for Qt 5 (5.9, 5.11 and dev)
  • Soon to be released!
    • Commercial/GPL2/LGPL3 licenses

What's next?

http://pyside.org

Find us on #qt-pyside on Freenode

Mailing list: https://bit.ly/pyside2

@cmaureir