Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Solved How to convert QIcon::fromTheme to Pixmap?

    General and Desktop
    3
    7
    236
    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 Kite R

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion @Kite R last edited by

        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 Reply Quote 1
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          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 Reply Quote 0
          • K
            Kite R @SGaist last edited by Kite R

            @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 Reply Quote 0
            • K
              Kite R last edited by Kite R

              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 Reply Quote 0
              • K
                Kite R last edited by

                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");
                
                mrjj 1 Reply Last reply Reply Quote 1
                • mrjj
                  mrjj Lifetime Qt Champion @Kite R last edited by

                  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 Reply Quote 1
                  • K
                    Kite R last edited by

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