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

How to convert QIcon::fromTheme to Pixmap?



  • I want to build a QIcon from theme icons, but how do you convert a QIcon::fromTheme() to pixmap? Everything I try doesn't seem to work.

    Is this wrong?

    QPixmap pm_off =
          QIcon::fromTheme(icon_off).pixmap(16);
      QPixmap pm_on =
          QIcon::fromTheme(icon_on).pixmap(16);
    
      QIcon icon;
      icon.addPixmap(pm_off, QIcon::Normal, QIcon::Off);
      icon.addPixmap(pm_on, QIcon::Normal, QIcon::On);
    
      return icon;
    

  • Lifetime Qt Champion

    Hi
    And you are on a linux / nix platform that has themes installed ?

    what does
    https://doc.qt.io/qt-5/qicon.html#isNull
    say ?


  • Lifetime Qt Champion

    Hi,

    You can generate a new icon based on the pixmaps from the themed icon except the ones you want to customize and then add your custom files.



  • @SGaist Hi, thanks for replying. I tried, but it doesn't seem to work, I updated my post with clearer example.



  • I tried this, but it doesn't work, what am I missing? I've looked at examples, and this is what I see, but for whatever reason it returns nothing. I've checked the first line works by setting that as the return value, but the other lines do nothing.

      QIcon icon_off = QIcon::fromTheme("fold_off");
    
      QIcon icon;
      icon.addPixmap(icon_off.pixmap(16, 16), QIcon::Normal, QIcon::Off);
    
      return icon_off; // WORKS
      return icon;     // DOES NOT WORK
    


  • So I tried this just to confirm that it would work with a normal QIcon and it does...

    QIcon icon = QIcon(":img/icon.svg");
    QPixmap pm = icon.pixmap(16, 16);
    
    QIcon newIcon;
    newIcon.addPixmap(pm, QIcon::Normal, QIcon::Off);
    return newIcon;
    

    Could someone please explain why changing the first line to this doesn't work?

    QIcon icon = QIcon::fromTheme("icon");
    

  • Lifetime Qt Champion

    Hi
    And you are on a linux / nix platform that has themes installed ?

    what does
    https://doc.qt.io/qt-5/qicon.html#isNull
    say ?



  • @mrjj said

    what does
    https://doc.qt.io/qt-5/qicon.html#isNull
    say ?

    No I'm on Windows, I use setThemeName. Thanks for the tip with isNull(), I completely overlooked it since it appeared to be working on the face of it (icons would show, they just wouldn't convert to pixmap), it turns out it was returning true in every case.

    I moved my search paths and set theme name code higher up in main.cpp, this changed isNull to false in every case. The program is huge (not mine) and main is about 1000 lines so I don't fully understand why that happened yet, but converting to QPixmap now works it seems.

    Thanks! I'll be back if it fails in my functions.


Log in to reply