Important: Please read the Qt Code of Conduct -


  • I am getting crazy with QIcon::fromTheme()... My goal is to get "standard" icons on Linux (with KDE or Gnome) and fallback custom icons for all other platforms.

    So in my main(), I have the following code (here only shown for Windows, but the same is also defined for Mac):

    //for Linux, we keep the icon theme of the user
    #ifdef Q_OS_WIN
    	QIcon::setThemeName("flat-bw"); //we provide our own icons for MS Win / osX

    Then in my /resources directory, I have a QRC file such as:

    <!DOCTYPE RCC><RCC version="1.0">
    <qresource prefix="icons/flat-bw">
    <file alias="index.theme">icons/flat-bw/index.theme</file>
    <file alias="48x48/document-open.png">icons/flat-bw/48x48/document-open.png</file>
    <file alias="48x48/application-exit.png">icons/flat-bw/48x48/application-exit.png</file>

    In my resources/icons/flat-bw directory, I have a "48x48" directory with the icons and an index.theme file such as:

    [Icon Theme]
    Comment=Breeze BW Icons

    And finally, where I need an icon, I get it as:

    file_open_ini_ = new QAction(QIcon::fromTheme("document-open"), tr("&Open INI file"), menu_file);

    On Linux, it works great. It picks up the user-configured default theme. If I "force" it to take my custom icons (by calling "setThemeName"), it also works. But On Windows, there is no way to get any icon. If I set the icon with a path (such as QIcon::fromTheme(":icons/flat-bw/48x48/document-open.png")), then it works. If I try to define a fallback icon, it never calls the fallback.

    Would anybody have any idea what could be wrong or how to figure out what could be wrong? In the resource browser of QtCreator, I properly see my icons... If I print the icons search path, it starts with ":icons" so it should be OK...
    Thanks a lot for any tip!

  • @MathiasGr said in QIcon::fromTheme:

    <qresource prefix="icons/flat-bw">

    try <qresource prefix="/icons/flat-bw"> instead

  • @VRonin I was really hopeful, but it makes no difference. I don't understand why the handling of icons is not much better in Qt, this is a feature that is required for all GUIs...

  • Did you re-run qmake after the change?

    This minimal example works for me, could you try it on your side?

  • @VRonin Thanks a lot for the code example! It works, so I have to find what I do differently that makes the problem... I'll post the results here when I will have figured it out. Again, thanks a lot for the code example!

Log in to reply