Qt for Python

qt.io/qt-for-python

About


Dr. Cristián Maureira-Fredes
Software Engineer @ The Qt Company

Agenda

  • Few words on Python
  • Overview of the Qt project
  • Qt for Python
    • Brief history and goals
    • Current status
    • Binding generation (Shiboken)
    • Examples
  • Q&A

Python

  • High-level language
  • General purpose
  • Dynamic typing
  • Multi paradigm
  • Code readability

What is Qt?


Cross-platform
application framework

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
  • Additional tools: QtCreator
  • 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 app development

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

We want to expose all that to Python

Project

  • PySide2
    • Set of modules for Python to use Qt
    • Allows to use Qt without the trouble of compiling
  • Shiboken
    • Python binding generation tool for C++
  • In development
    • Integration with QtCreator
    • Python application deployment

Timeline

  • 2008: Started out as PySide for Qt4
  • 2009: Released by Nokia
  • 2010: Development started to grow
  • 2013: Slow-down in the development frequency
  • 2015: Effort to port it to Qt5 on Github

Currently

  • 2016: Developed by "The Qt Company"
  • 2018: Technical preview released (Qt 5.11)
  • Goal become a first class citizen in Qt5

How does it work?

Shiboken

  • Generator that outputs C++ code (Python extensions)
  • Inspired by boost::Python
  • Non-Qt libraries can be wrapped too!.

How does PySide2 look like?

Hello World


import sys
from PySide2.QtWidgets import QApplication, QLabel

# Qt Application
app = QApplication(sys.argv)

# Qt Widget
label = QLabel("Hello World!")
label.show()

# Executing app
sys.exit(app.exec_())
    

Simple browser


import sys
from PySide2.QtCore import QUrl
from PySide2.QtWidgets import QApplication, QLineEdit, QMainWindow, QToolBar
from PySide2.QtWebEngineWidgets import QWebEngineView

class Browser(QMainWindow):
    def __init__(self):
        super(Browser, self).__init__()
        url = 'https://www.qt.io'
        self.toolbar = QToolBar()
        self.addToolBar(self.toolbar)
        self.address = QLineEdit()
        self.address.setText(url)
        self.address.returnPressed.connect(self.load)
        self.toolbar.addWidget(self.address)
        self.view = QWebEngineView()
        self.view.load(QUrl(url))
        self.view.page().urlChanged.connect(self.url_changed)
        self.setCentralWidget(self.view)

    def load(self):
        url = QUrl.fromUserInput(self.address.text())
        if url.isValid():
            self.view.load(url)

    def url_changed(self, url):
        self.address.setText(url.toString())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = Browser()
    main_window.show()
    sys.exit(app.exec_())
    

Qt Modules

  • QtCore
    • Core non-graphical classes used by other modules
  • Qt GUI
    • Base classes for UI components
  • QtWidgets
    • Classes to extend Qt GUI with C++ Widgets
  • Additional
    • QtCharts, QtMultimedia, QtNetwork, QtQuick, Qt Test, etc.

Coding style

  • Python uses underscore_variable names
  • Qt uses camelCase names
  • Preserve Qt API
    • Easy to move from C++ to Python
    • Qt documentation
  • Using both styles:
    • title_label.setText("Hello")

Examples

  • Simple button
  • Media player
  • QML
  • QtCharts
  • scriptable application

Why Qt for Python?

  • The simplicity and robustness of Python
  • Easily connect Qt with Python modules
  • Fast prototyping (Cross-platform)
  • Extend applications with scripts
  • Qt project goodies

Status

  • Technical preview for Qt 5.11
    • Available in PyPi:
      • pip install PySide2
    • 5.11.1 released!
  • Planned official release for Qt 5.12
    • Commercial and GPL2/LGPL3 licenses

Q&A

Find us on #qt-pyside on Freenode
Mailing list: bit.ly/pyside2

qt.io/qt-for-python
pyside.org