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. How can I assign QIcon roles using QIcon::fromTheme?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 186 Views 1 Watching
  • 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 Offline
    K Offline
    Kite R
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0

      • Login

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