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. Set inactive icon for action shown as a button in a toolbar

Set inactive icon for action shown as a button in a toolbar

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 5.9k Views
  • 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.
  • A Offline
    A Offline
    A1exander_Z
    wrote on last edited by
    #1

    I have an application with a toolbar populated with QAction objects. I can set the default (active) icons with QAction::setIcon(QIcon(path_to_icon_file)) method. I also would like to set the deisbled ("greyed out") icon for the actions. Theres is one "discussion I have found":http://stackoverflow.com/questions/1358563/set-a-custom-icon-for-a-qaction-when-disabled, but the approach described there does not work. The code with QAction::icon().addPixmap(QPixmap(path_to_icon_file), QIcon::Disabled) calls compiles and runs without errors, but the new icons are not displayed (instead of them there are grayscale versions of the default icons, as usual). Is it possible to set custom image for a disabled icon in QToolBar?

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

      Hi,

      What exact error are you getting ?

      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
      • A Offline
        A Offline
        A1exander_Z
        wrote on last edited by
        #3

        There are no errors. The code boils down to the following:

        @action->setIcon(QIcon(":/icons/Create-Active.svg"));
        action->icon().addPixmap(QPixmap(":/icons/Create-Inactive.svg"), QIcon::Disabled);@

        The first line works as expected, the action shows the corresponding image (Create-Active.svg). The second line apparently does nothing, because the disabled action shows grayscale version of Create-Active.svg, not Create-Inactive.svg.

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

          you should rather use something like:

          @
          QIcon myIcon(":/icons/Create-Active.svg");
          myIcon.addPixmap(QPixmap(":/icons/Create-Inactive.svg"), QIcon::Disabled);
          action->setIcon(myIcon);
          @

          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
          • A Offline
            A Offline
            A1exander_Z
            wrote on last edited by
            #5

            Interesting. It works correctly in Windows, but not on Android (the code is the same).

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              [quote author="SGaist" date="1424133957"]you should rather use something like:

              @
              QIcon myIcon(":/icons/Create-Active.svg");
              myIcon.addPixmap(QPixmap(":/icons/Create-Inactive.svg"), QIcon::Disabled);
              action->setIcon(myIcon);
              @[/quote]

              Just to explain why: the action->icon() call returns a copy of the QIcon instance set on the action. Then you modify the copy, and throw that modified copy away. The icon set on the action is not modified in the process.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                A1exander_Z
                wrote on last edited by
                #7

                Thanks for the explanation. It is interesting why this code does not work on Android...

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  A1exander_Z
                  wrote on last edited by
                  #8

                  I have tested the code under Windows, Linux, Mac OS X and Android. It works everywhere except Android.

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

                    Maybe a silly question but does Android use the disabled state ?

                    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
                    • A Offline
                      A Offline
                      A1exander_Z
                      wrote on last edited by
                      #10

                      I do not know. The situation with icons for disabled QAction states is strange. I have just found that setting icon for the disabled state in Qt Designer does not work (both in Windows and Linux). Setting the icon through the code posted above (used in the window's constructor) works as expected.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        A1exander_Z
                        wrote on last edited by
                        #11

                        It gets stranger and stranger. I have a QAction with default icon (Create-Active.svg image). When I set an icon for the disabled state (actually it is QIcon::Mode, value QIcon::Disabled, state is QIcon::Off) in Qt Designer (Create-Inactive.svg image), it is correctly shown in the property editor on the right. However, in the pop-up dialog shown when I double-click the action in the list of actions the same icon is shown in grayscale (grayscale version of Create-Inactive.svg image). When I run the program, again I see the grayscale version of the disabled icon. If I add pixmap thorough the following code in window's constructor

                        @QIcon icon(":/icons/Create-Active.svg");
                        icon.addPixmap(QPixmap(":/icons/Create-Inactive.svg"), QIcon::Disabled);
                        action->setIcon(icon);@

                        it works properly (I see Create-Inactive.svg image, not its grayscale version). It is the same under Windows and Linux.

                        Under Android it is even stranger. If I set the icon in the designer, the behavior is the same as under Windows or Linux (grayscale version of Create-Inactive.svg). So Android does use the disabled mode somehow. But, if I set Create-Inactive.svg icon in the code (with or without previously setting it in the designer), I see grayscale version of the default icon (Create-Active.svg). So setting it in the code somehow resets setting in the designer.

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          A1exander_Z
                          wrote on last edited by
                          #12

                          After looking through QIcon class sources I have found that the problem is related to icon size. If I render SVG image to pixmap of the correct size and then add it, it works under Android too:

                          @icon = QIcon(":/icons/Create-Active.svg");
                          QImage image = size, QImage::Format_ARGB32_Premultiplied);
                          image.fill(Qt::transparent);
                          QSvgRenderer r(QString(":/icons/Create-Inactive.svg"));
                          QPainter p(&image);
                          r.render(&p);
                          icon.addPixmap(QPixmap::fromImage(image), QIcon::Disabled);
                          action->setIcon(icon);@

                          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