Plugin migration to be compatible with Qt5 and Qt6 - qgis/QGIS GitHub Wiki

Qt6 migration

It's possible to make a plugin for both Qt5 and Qt6.

Script

To run this script, you need QGIS with Qt6.

  1. Install Python dependencies:

    pip install astpretty tokenize-rt
    
  2. Install additionnal system dependencies. Typically on Debian based-images:

    sudo apt install python3-pyqt6 python3-pyqt6.qtsvg python3-pyqt6.qsci
    
  3. Download the pyqt5_to_pyqt6 script

  4. You should check that PyQt5 is not available in the Python environment, the script will work better

  5. pyqt5_to_pyqt6.py /path/to/plugin

  6. Edit the metadata.txt by adding supportsQt6=True

This will get you in the right direction but might not do all necessary changes. Make sure to test your plugin thoroughly. Consider using an IDE with inspection that can notify you about broken imports, bad usage or non-existent references.

Use Docker image

A bundled image with QGIS based on Qt6, including Oracle client and PDAL and the migration script is available: https://gitlab.com/Oslandia/qgis/pyqgis-4-checker. It can be used like this:

docker run --rm -v "$(pwd):/home/pyqgisdev/" registry.gitlab.com/oslandia/qgis/pyqgis-4-checker/pyqgis-qt-checker:latest pyqt5_to_pyqt6.py --logfile /home/pyqgisdev/pyqt6_checker.log .

CI

The Docker image can also be used in a CI context to ensure compatibility along the development life-cycle.

GitLab CI

stages:
  - 🐍 lint

variables:
  PROJECT_FOLDER: "{plugin_folder_path_within_git_repository}"

lint:qt6:
  stage: 🐍 lint
  image: registry.gitlab.com/oslandia/qgis/pyqgis-4-checker/pyqgis-qt-checker:latest
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      changes:
        - "$PROJECT_FOLDER/**/*.py"
        - ".gitlab-ci.yml"
      when: on_success
    - when: never
  script:
    # just print the script help
    # - pyqt5_to_pyqt6.py --help
    # first, a dry run to get the log file
    - pyqt5_to_pyqt6.py --dry_run --logfile pyqt6_checker.log $PROJECT_FOLDER/
    # then running with edit enabled to
    - pyqt5_to_pyqt6.py $PROJECT_FOLDER/
  artifacts:
    paths:
      - pyqt6_checker.log
    when: always
    access: all
    expire_in: 3 days

Official C++ documentation

Differences Between PyQt6 and PyQt5 (Riverbank Computing)

https://www.riverbankcomputing.com/static/Docs/PyQt6/pyqt5_differences.html "gives an overview of the differences between PyQt6 and PyQt5. This is not an exhaustive list and does not go into the detail of the differences between the Qt v6 and Qt v5 APIs."

Manual conversion

To make a code working for both versions, it's mostly about how enums are called. For instance, according to Qt Namespace :

Working only in PyQt5 Working for both PyQt5 and PyQt6
Qt.UserRole Qt.ItemDataRole.UserRole
Qt.WaitCursor Qt.CursorShape.WaitCursor
Qt.blue Qt.GlobalColor.blue
etc. etc.