Desktop App support - Qt5


#1

This guide explains the recommended way to make a qt5 application function correctly inside a snap. These instructions need to be applied in addition to the instructions for creating a snap in your programming language. For example, if your application uses the C programming language and Qt5, you also need to follow the instructions for snapping a C/C++ application.

The Snapcraft Desktop Helpers project provides the desktop-qt5 part to include the Qt5 dependencies in your snap. It also includes the desktop-launch script to initialise the desktop-specific functionality before your application launches.

1. Add qt5 dependencies to your snap

Copy the definition of the desktop-qt5 part from the Snapcraft Desktop Helpers snapcraft.yaml into your Snapcraft recipe, in the parts section. Change the source property of this part to https://github.com/ubuntu/snapcraft-desktop-helpers.git.

  # This part installs the qt5 dependencies and a `desktop-launch` script to initialise
  # desktop-specific features such as fonts, themes and the XDG environment.
  # 
  # It is copied straight from the snapcraft desktop helpers project. Please periodically
  # check the source for updates and copy the changes.
  #    https://github.com/ubuntu/snapcraft-desktop-helpers/blob/master/snapcraft.yaml
  # 
  desktop-qt5:
    source: https://github.com/ubuntu/snapcraft-desktop-helpers.git
    source-subdir: qt
    ...
    <copy the rest of this part from the desktop helpers snapcraft.yaml>
    ...

NOTE: In order to keep the integration up-to-date for the best user experience, you should check if the definition in the Snapcraft desktop helpers repo has been changed periodically.

2. Initialise desktop-specific functionality

Insert bin/desktop-launch into the app’s command chain.

apps:
  qt5app:
    command: bin/desktop-launch qt5app

If you’re using the full adapter, insert the bin/desktop-launch launcher into the list of the command-chain key of the apps._app_name_ property instead:

apps:
  qt5app:
    adapter: full
    command: bin/qt5app
    command-chain:
    - bin/desktop-launch

NOTE: If your application requires recent Qt or KDE libraries that aren’t available from Ubuntu LTS, the kde-frameworks content and SDK snaps might be useful. Please take a look at KDE Snap Guide to see how to use this SDK. This SDK only supports the AMD64 processor architecture.

3. [optional] Add GTK theme integration

Snapped Qt applications by default don’t use the UI theme of the operating system they are running on. You can make your snapped application adapt to most common UI themes using the gtk-common-themes content snap and the gtk3 Qt Platform theme.

Add the following qt5-gtk-platform part into the parts section of your snapcraft.yaml file.

  qt5-gtk-platform:
    plugin: nil
    stage-packages:
      - qt5-gtk-platformtheme

In the apps._app_name_ stanza, merge the following environment property:

apps:
  qt5app:
    environment:
      # Use GTK3 cursor theme, icon theme and open/save file dialogs.
      QT_QPA_PLATFORMTHEME: gtk3

In the plugs stanza, add the following plug definition:

plugs:
  # Support for common GTK themes
  # https://forum.snapcraft.io/t/how-to-use-the-system-gtk-theme-via-the-gtk-common-themes-snap/6235
  gsettings:
  gtk-3-themes:
    interface: content
    target: $SNAP/data-dir/themes
    default-provider: gtk-common-themes
  icon-themes:
    interface: content
    target: $SNAP/data-dir/icons
    default-provider: gtk-common-themes
  sound-themes:
    interface: content
    target: $SNAP/data-dir/sounds
    default-provider: gtk-common-themes

Now your applications will use the icon theme, cursor theme and open/save file dialogs of the desktop it runs on. The colors of your applications will also adapt to the GTK theme colors of the operating system.

Examples

You can take a look at the snapcraft.yaml file of keepassxc for a complete example of a qt5 snap.

References


Desktop App support - Qt
Snap documentation
Theme issue with content snap
Snap documentation