Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt icon theme switch
QtWS25 Last Chance

Qt icon theme switch

Scheduled Pinned Locked Moved Solved General and Desktop
qssiconthemestylesheets
4 Posts 2 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SavizS Offline
    SavizS Offline
    Saviz
    wrote on last edited by
    #1

    When searching on the internet for topics such as the following:

    1- "How to switch themes in QtWidget applications"
    2- "How to create Dark mode and Light mode for QtWidget applications"
    Etc.

    One often finds solutions that consist of the following code:

    Code:

    QFile file(path);
    
     if(file.open(QIODevice::ReadOnly | QIODevice::Text))
     {
          QTextStream input(&file);
    
          application.setStyleSheet(input.readAll());
     }
    

    This code attempts to read a stylesheet qss file and apply the styles to our application widgets. Although this is a very nice and convenient way to approach this problem, But it still doesn't fix one big issue:

    How does one switch the icons for a theme? (What I mean by this questions is the following)

    Let's assume we have a set of light theme icons setup for some QPushButtons/QActions in our application. If we want to switch to another theme (dark theme), then we can simply use the code mentioned above to switch the look and feel of our Widgets by applying a different stylesheet. However, the icons remain the same style. Therefore, I have two questions:

    1 - Is there a way to set the icons for QPushButtons/QActions through stylesheets? (Is there an element like img/icon that I can use to specify the URL?)

    2- If the above is not possible, then what is a good way to approach this problem?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You might want to check this site for a pretty interesting article about Qt and its theme support.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      SavizS 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        You might want to check this site for a pretty interesting article about Qt and its theme support.

        SavizS Offline
        SavizS Offline
        Saviz
        wrote on last edited by
        #3

        @SGaist Thank you for sharing this amazing article.

        Unfortunately, I am facing another issue that I cannot solve. After reading this article I attempted to give it a try with a simple example. I found a very nice icon theme called Qogir from the Gnome website.

        Link to icon theme:
        (https://www.gnome-look.org/p/1296407)

        I followed the instructions about how to import these files into the resource file. I choose only two files since this is only a test. In addition, I also included the index.theme files for each folder.

        Here is how my resource file looks:

        <RCC>
            <qresource prefix="/">
                <file>resources/icons/Qogir/24/actions/accept_signal.svg</file>
                <file>resources/icons/Qogir/index.theme</file>
                <file>resources/icons/Qogir-dark/16/actions/accept_signal.svg</file>
                <file>resources/icons/Qogir-dark/index.theme</file>
            </qresource>
        </RCC>
        

        I created a simple QPushButton and added the Qogir-dark icon to it for demonstration purposes.
        (The dark icons are displayed as white, which makes them ideal for darker backgrounds)

        Here is how my button looks with this icon:

        Button light.png

        I added the code necessary to make the theme switch.

        main.cpp:

        #include "mainwindow.h"
        #include <QApplication>
        #include <QDebug>
        
        int main(int argc, char *argv[])
        {
            QApplication application(argc, argv);
        
            MainWindow mainWindow;
            mainWindow.show();
        
            // Making sure that Qt is looking in the correct path
            QStringList list;
            list<<"://resources/icons";
            QIcon::setThemeSearchPaths(list);
            
            QIcon::setThemeName("Qogir");
        
            return application.exec();
        }
        

        I was expecting that as soon as my application runs, the icon would be switched to the light version.
        (The light icons are displayed as black).

        Unfortunately, this did not happen. The icon remains the same. I even tried to reverse the order of icons to see if I was mistaking with the colors. But the result was the same. Nothing was changing.

        As I am not experienced enough, I cannot tell what is going wrong. I would be very grateful if you could help me with this problem.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Sorry for the very late reply, would it be possible to have a complete minimal compilable project to be able to test your issue ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • SavizS Saviz has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved