Skip to content

Icons from theme are not displayed in QtWidgets Application #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tastytea opened this issue Oct 31, 2018 · 7 comments
Closed

Icons from theme are not displayed in QtWidgets Application #17

tastytea opened this issue Oct 31, 2018 · 7 comments
Assignees

Comments

@tastytea
Copy link

tastytea commented Oct 31, 2018

I have a program which I package as a dpkg and as an AppImage, from the same source. The binary from the dpkg shows icons in the toolbar, the AppImage does not. I use icon names specified in the Icon Naming Specification, like QIcon::fromTheme("list-add"). I do not modify any environment variables for the AppImage.

In Ubuntu 18.04 no icons are shown, in Gentoo there are icons but not from the system theme.

AppImage: https://schlomp.space/attachments/15cba25f-e5a0-4c14-b08b-df35f8a4672e

.ui-file where I specify the icons: https://schlomp.space/tastytea/whyblocked/src/branch/master/src/whyblocked.ui#L141

The generated code looks like this:

QIcon icon1;
iconThemeName = QStringLiteral("list-add");
if (QIcon::hasThemeIcon(iconThemeName)) {
    icon1 = QIcon::fromTheme(iconThemeName);
} else {
    icon1.addFile(QStringLiteral("."), QSize(), QIcon::Normal, QIcon::Off);
}
action_add->setIcon(icon1);

Edit: Icons are shown in Fedora 28, no icons in Elementary 5.

@tastytea
Copy link
Author

I built the AppImage in a debian stretch docker container via drone, this is the recipe: https://schlomp.space/tastytea/whyblocked/src/branch/master/.drone.yml

@tastytea
Copy link
Author

I have the same problem with the QtWidgetsApplication in linuxdeploy/linuxdeploy-plugin-qt-examples.

@azubieta
Copy link
Collaborator

azubieta commented Oct 31, 2018

An AppImage should include all its dependencies. Not only shared libraries but also other resources such as icons. There is no warranty that the icons that you use will be in every Linux system, therefore, you must also embed them.

Two options came to my mind to achieve it:

  1. Embed the whole icon theme or a portion of it (but it must remain a valid icon theme). Create a script to properly export the XDG_DATA_DIRS environment variable and execute your binary.
  2. Just copy the icons and modify your application to properly load them as a fallback.

Still, we must find out why icons from theme are not being loaded properly.

@azubieta
Copy link
Collaborator

For more information of the "Option 1" see https://stackoverflow.com/questions/50918398/geting-icon-by-name-is-not-working

@azubieta
Copy link
Collaborator

azubieta commented Nov 1, 2018

After a lot of testing, I found that there are several plugins that affect this functionality. Those are:

Therefore if you want that your app to properly show the icons from the theme on every desktop environment you will have to include all of the above plugins. In the case of the platformthemes, gathering all existent and future plugins is just impossible. Therefore you will also have to provide a fallback icon theme.

Currently modifying the QtWidgetsApplication in order to demonstrate a proper way of doing it.

@azubieta azubieta self-assigned this Nov 1, 2018
@azubieta azubieta added the bug Something isn't working label Nov 1, 2018
@azubieta azubieta changed the title Icons are not displayed Icons are not displayed in QtWidgets Application Nov 1, 2018
@azubieta azubieta changed the title Icons are not displayed in QtWidgets Application Icons from theme are not displayed in QtWidgets Application Nov 1, 2018
azubieta added a commit to linuxdeploy/linuxdeploy-plugin-qt-examples that referenced this issue Nov 1, 2018
@azubieta
Copy link
Collaborator

azubieta commented Nov 1, 2018

Sadly we cannot warranty that all the imageformats plugins available in the world are included nor the platformtheme plugins nor the icons in the target platform icons theme. Therefore, you should always provide fallback icons in your application.

You can find an example of how to do it in this project QtWidgetsApplication

In short, load your icons this way:
QIcon addIcon = QIcon::fromTheme("list-add", QIcon(":/scalable/list-add.svg"));

@azubieta
Copy link
Collaborator

azubieta commented Nov 1, 2018

If your application requires additional plugins you can always use the environment variable EXTRA_QT_PLUGINS. This way:

$EXTRA_QT_PLUGINS=pluginA;pluginB;

@azubieta azubieta removed the bug Something isn't working label Nov 1, 2018
@azubieta azubieta closed this as completed Nov 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants