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 to convert QIcon::fromTheme to Pixmap?
Forum Updated to NodeBB v4.3 + New Features

How to convert QIcon::fromTheme to Pixmap?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.0k Views 2 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 Kite R
    #1

    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;
    
    1 Reply Last reply
    0
    • K Kite R

      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");
      
      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #6

      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 ?

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

        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.

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

        K 1 Reply Last reply
        0
        • SGaistS SGaist

          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.

          K Offline
          K Offline
          Kite R
          wrote on last edited by Kite R
          #3

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

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kite R
            wrote on last edited by Kite R
            #4

            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
            
            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kite R
              wrote on last edited by
              #5

              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");
              
              mrjjM 1 Reply Last reply
              1
              • K Kite R

                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");
                
                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #6

                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 ?

                1 Reply Last reply
                1
                • K Offline
                  K Offline
                  Kite R
                  wrote on last edited by
                  #7

                  @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.

                  1 Reply Last reply
                  1

                  • Login

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