Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I assign QIcon roles using QIcon::fromTheme?



  • Hi, I'm still fairly new to Qt, any help is appreciated!

    I have a custom QIcon, like so:

    QIcon customIcon(const char *name, bool withOver) {
      QString on   = QString(":img/") + name + "_on.png";
      QString off  = QString(":img/") + name + "_off.png";
      QString over = QString(":img/") + name + "_over.png";
    
      QIcon ico;
      ico.addFile(off, QSize(), QIcon::Normal, QIcon::Off);
      ico.addFile(on, QSize(), QIcon::Normal, QIcon::On);
      if (withOver)
        ico.addFIle(over, QSize(), QIcon::Active);
      else
        ico.addFile(on, QSize(), QIcon::Active);
    
      return ico;
    }
    

    However I want to use icons from QIcon::fromTheme to allow the user to switch themes. I'm having trouble rebuilding this custom icon in the same way, as there appears to be no way to set roles? I can't quite figure it out. Here's what I see in my head (this won't work obviously, but just for example):

    QIcon customIcon(const char *name, bool withOver) {
      QString on   = QString(name) + "_on.png";
      QString off  = QString(name) + "_off.png";
      QString over = QString(name) + "_over.png";
    
      QIcon ico = QIcon::fromTheme(name);
      ico.addFile(off, QSize(), QIcon::Normal, QIcon::Off);
      ico.addFile(on, QSize(), QIcon::Normal, QIcon::On);
      if (withOver)
        ico.addFIle(over, QSize(), QIcon::Active);
      else
        ico.addFile(on, QSize(), QIcon::Active);
    
      return ico;
    }
    

    I want to take all the pseudo state files in the theme (on, off, over) and convert them to a QIcon, I hope that makes sense!


  • Lifetime Qt Champion

    Hi,

    You are likely adding states that have already been filled. Therefore when the painting happens, the originally added files/pixmap are found first and used.


Log in to reply