Using PyQt in Qt Creatorsø. 26 februar 2017
PyQt and PySide are Python bindings for Qt. I will focus mainly on PyQt in this post, but most of what I write is also true for PySide. Both are a common choices for adding a GUI to a Python application.
Using PyQt instead of the original C++ library makes it easy to deploy pure QML applications to a wider audience through Anaconda packages.
Because writing QML applications is best done in Qt Creator, this guide explains how to set up Qt Creator to work well with a PyQt project.
Installation and setup
On other systems (or if you are reading this after the above issue has been fixed), Anaconda comes prepackaged with PyQt, so there is no need to install it with pip.
pip install pyqt5
Adding an Anaconda kit to Qt Creator
First, let us add a kit based on your PyQt installation. This is not strictly necessary, but ensures that the code completion will be based on your currently installed version of PyQt and not the version you installed from qt.io.
- Select Tools > Options > Build & Run > Qt Versions > Add.
- Find the path to your Anaconda installation and select
bindirectory of the installation
- Click Apply.
- Select Kits in the same view and click Add.
- Rename the new kit to PyQt and change the Qt Version to the one you just added.
Adding a main file
Second, open your project or create a new one. Add a main.py file if you don't have one from before. If you are running a default QML project, it should look something like this:
import sys if sys.platform == "linux" or sys.platform == "linux2": # TODO remove this OpenGL fix when PyQt # doesn't require OpenGL to be loaded first. # NOTE This must be placed before any other imports! import ctypes from ctypes.util import find_library libGL = find_library("GL") ctypes.CDLL(libGL, ctypes.RTLD_GLOBAL) from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtQml import * from PyQt5.QtWidgets import * import qml_qrc def main(): app = QApplication(sys.argv) engine = QQmlApplicationEngine() engine.load(QUrl("qrc:/main.qml")) return app.exec_() if __name__ == "__main__": sys.exit(main())
Note the fix that loads the GL library befre Qt on Linux systems. This is because of a bug that occurs with Nvidia drivers on Ubuntu.
Building resource files with pyrcc5
Third, change the build settings of your project (Ctrl+4). You need to add a build step that runs
<Anaconda install>/bin/pyrcc5 -o mypackage/qml_qrc.py mypackage/qml.qrc
If you have multiple .qrc files a build step needs to be added for each of those.
Finally, add a new Run configuration for your project.
This should run a custom executable, which is the
python executable found in
Anaconda's bin directory.
The argument should be
Change the run directory to
With all this in place, you should be able to develop your PyQt project using Qt Creator.
Depoloying to Anaconda Cloud
With your development environment set up, it is now possible to create a
conda package that can be uploaded to
Here is a simple
meta.yaml template that can be used to create a conda package:
package: name: mypackage version: 1.0 build: entry_points: - mypackage = mypackage.main:main source: path: . requirements: build: - python - setuptools - pip run: - python - setuptools - pip - pyqt >=5 test: imports: - mypackage about: home: https://github.com/CINPLA/exdir-browser/ license: GPLv3 license_file: LICENSE
This workflow could be improved by
- modifying the project's .pro (or .qbs) file to run pyrcc5 automatically for each .qrc file.
- adding a plugin to Qt Creator that parses custom types registered by
main.py, similar to what is done in C++.
But even with these improvements missing, this setup makes it way easier to work with a QML/Python project in Qt Creator because of the added autocompletion.