Skip to the content.

Porting projects to Qt5 and Python3

We have chosen to go for a compatible sources set, which can work with Qt4 or Qt5, Python2 or Python3. Porting to Qt5 and to Python3 are two independent tasks.

Projects list

How to setup a build workflow using Qt5 and/or python3

Building

Running

To run programs using python3, either run explicitly python3:

python3 <build_workflow>/bin/morphologist

But this is not very convenient, and many python scripts start with the standard shebang:

#!/usr/bin/env python

which would normally call python2 because python2 is what is called via the python command on many systems up to now. A simple fix for that is to make a symlink from python3 to python in the bin directory of the build workflow:

ln -s /usr/bin/python3 <builmd_workflow>/bin/python

Then once the build-workflow is setup (bv_env.sh has been sourced from this build workflow) and it is in the paths, the python command will switch to python3, and all python scripts will do the same.

Qt4 / Qt5

imports:

Don’t import directly PyQt5 or PyQt4. We have a compatibility module: soma.qt_gui.qt_backend which replaces the explicit imports:

    from soma.qt_gui.qt_backend import Qt
    widget = Qt.QWidget()
    # etc.

The qt_backend module supports PyQt4, PyQt5 and PySide transparently, and offers a few functions to help links with matplotlib (see init_matplotlib_backend) or for the few functions which do not have the same API in the different implementations (like QFileDialog.getOpenFileName). If you need to explicitly initialize the backend to a specific Qt implementation, do it only in main scripts directly called from the user, not in modules/libraries which may be imported.

    from soma.qt_gui import qt_backend
    qt_backend.set_qt_backend('PyQt5')
    from soma.qt_gui.qt_backend import Qt
    widget = Qt.QWidget()
    # etc.

But the best way is not to force this, and rely on the QT_API environment variable that any user can set:

    export QT_API=pyqt5
    soma_workflow_gui # will use PyQt5.

The QT_API variable is already set by the bv_env program according to the settings of the build workflow, so you even don’t have to care about it in bv_makerand casa-distro.

major changes:

Python2 / python3

official porting doc

Things to check: