Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    2
    2
    53
    Loading More Posts
    • 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.
    • K
      Kite R last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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.

        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 Reply Quote 0
        • First post
          Last post