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. Icon disappears when clicked - How can I set multiple QIcon::Modes for an icon?
QtWS25 Last Chance

Icon disappears when clicked - How can I set multiple QIcon::Modes for an icon?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 4.0k 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.
  • S Offline
    S Offline
    skebanga
    wrote on last edited by skebanga
    #1

    I have a png format image of a play button in which I have stored as an embedded resource in my application.

    <!DOCTYPE RCC><RCC version="1.0">
      <qresource prefix="icon">
        <file>play.png</file>
      </qresource>
    </RCC>
    

    I have created a QIcon with the same source image set for both Normal and Active modes

    QIcon play;
    play.addFile(":icon/play.png", QSize(), QIcon::Normal);
    play.addFile(":icon/play.png", QSize(), QIcon::Active);
    

    From what I understand, this should display the icon when unclicked (Normal mode), and continue doing so when clicked (Active mode)

    QIcon::Normal: Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available.

    QIcon::Active: Display the pixmap when the functionality represented by the icon is available and the user is interacting with the icon, for example, moving the mouse over it or clicking it.

    However, when I click on it it disappears (a blank box being displayed instead).

    Here is the icon unclicked

    alt text

    Here is the icon clicked

    alt text

    Minimal working example:

    I have created a minimal example replicating the behaviour I see

    #include <QApplication>
    #include <QMainWindow>
    #include <QMenuBar>
    
    void initIcons()
    {
    	Q_INIT_RESOURCE(view);
    }
    
    int main(int argc, char** argv)
    {
    	QApplication* app = new QApplication(argc, argv);
    	QMainWindow*  window = new QMainWindow();
    
    	QMenuBar* menu = new QMenuBar();
    	window->setMenuBar(menu);
    
    	QIcon play;
    	play.addFile(":icon/play.png", QSize(), QIcon::Normal);
    	play.addFile(":icon/play.png", QSize(), QIcon::Active);
    
    	QAction* action = new QAction(play, "", nullptr);
    	menu->addAction(action);
    
    	window->show();
    	return app->exec();
    }
    

    Update:

    I have also tried every combination of Mode and State with no change in behaviour:

    play.addFile(":icon/play.png", QSize(), QIcon::Normal,   QIcon::On);
    play.addFile(":icon/play.png", QSize(), QIcon::Normal,   QIcon::Off);
    play.addFile(":icon/play.png", QSize(), QIcon::Selected, QIcon::On);
    play.addFile(":icon/play.png", QSize(), QIcon::Selected, QIcon::Off);
    play.addFile(":icon/play.png", QSize(), QIcon::Active,   QIcon::On);
    play.addFile(":icon/play.png", QSize(), QIcon::Active,   QIcon::Off);
    

    I also tried addPixmap

    QIcon play;
    play.addPixmap(QPixmap(":icon/play.png"), QIcon::Normal,   QIcon::On);
    play.addPixmap(QPixmap(":icon/play.png"), QIcon::Normal,   QIcon::Off);
    play.addPixmap(QPixmap(":icon/play.png"), QIcon::Selected, QIcon::On);
    play.addPixmap(QPixmap(":icon/play.png"), QIcon::Selected, QIcon::Off);
    play.addPixmap(QPixmap(":icon/play.png"), QIcon::Active,   QIcon::On);
    play.addPixmap(QPixmap(":icon/play.png"), QIcon::Active,   QIcon::Off);
    

    None of these alter the behaviour at all unfortunately.

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

      Hi,

      AFAIK, you have to do it at the icon level. You can use the QIcon::addPixmap or QIcon::addFile method for that.

      Hope it helps

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

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        AFAIK, you have to do it at the icon level. You can use the QIcon::addPixmap or QIcon::addFile method for that.

        Hope it helps

        S Offline
        S Offline
        skebanga
        wrote on last edited by
        #3

        @SGaist thanks for the suggestion.

        I tried using addFile

        QIcon play;
        play.addFile(":icon/play.png", QSize(), QIcon::Normal);
        play.addFile(":icon/play.png", QSize(), QIcon::Active);
        QAction* action  = new QAction(play, "");	
        menu->addAction(action);
        

        Unfortunately the same effect occurs (icon visible when unclicked, black square when clicked)

        I thought perhaps I was using the wrong Mode, so I tried Selected too:

        play.addFile(":icon/play.png", QSize(), QIcon::Normal);
        play.addFile(":icon/play.png", QSize(), QIcon::Selected);
        play.addFile(":icon/play.png", QSize(), QIcon::Active);
        

        I then tried tried every combination of Mode and State:

        play.addFile(":icon/play.png", QSize(), QIcon::Normal, QIcon::On);
        play.addFile(":icon/play.png", QSize(), QIcon::Normal, QIcon::Off);
        play.addFile(":icon/play.png", QSize(), QIcon::Selected, QIcon::On);
        play.addFile(":icon/play.png", QSize(), QIcon::Selected, QIcon::Off);
        play.addFile(":icon/play.png", QSize(), QIcon::Active, QIcon::On);
        play.addFile(":icon/play.png", QSize(), QIcon::Active, QIcon::Off);
        

        I also tried QPixmap

        QIcon play;
        play.addPixmap(QPixmap(":icon/play.png"), QIcon::Normal, QIcon::On);
        play.addPixmap(QPixmap(":icon/play.png"), QIcon::Normal, QIcon::Off);
        play.addPixmap(QPixmap(":icon/play.png"), QIcon::Selected, QIcon::On);
        play.addPixmap(QPixmap(":icon/play.png"), QIcon::Selected, QIcon::Off);
        play.addPixmap(QPixmap(":icon/play.png"), QIcon::Active, QIcon::On);
        play.addPixmap(QPixmap(":icon/play.png"), QIcon::Active, QIcon::Off);
        

        None of these alter the behaviour at all unfortunately.

        A 1 Reply Last reply
        0
        • S skebanga

          @SGaist thanks for the suggestion.

          I tried using addFile

          QIcon play;
          play.addFile(":icon/play.png", QSize(), QIcon::Normal);
          play.addFile(":icon/play.png", QSize(), QIcon::Active);
          QAction* action  = new QAction(play, "");	
          menu->addAction(action);
          

          Unfortunately the same effect occurs (icon visible when unclicked, black square when clicked)

          I thought perhaps I was using the wrong Mode, so I tried Selected too:

          play.addFile(":icon/play.png", QSize(), QIcon::Normal);
          play.addFile(":icon/play.png", QSize(), QIcon::Selected);
          play.addFile(":icon/play.png", QSize(), QIcon::Active);
          

          I then tried tried every combination of Mode and State:

          play.addFile(":icon/play.png", QSize(), QIcon::Normal, QIcon::On);
          play.addFile(":icon/play.png", QSize(), QIcon::Normal, QIcon::Off);
          play.addFile(":icon/play.png", QSize(), QIcon::Selected, QIcon::On);
          play.addFile(":icon/play.png", QSize(), QIcon::Selected, QIcon::Off);
          play.addFile(":icon/play.png", QSize(), QIcon::Active, QIcon::On);
          play.addFile(":icon/play.png", QSize(), QIcon::Active, QIcon::Off);
          

          I also tried QPixmap

          QIcon play;
          play.addPixmap(QPixmap(":icon/play.png"), QIcon::Normal, QIcon::On);
          play.addPixmap(QPixmap(":icon/play.png"), QIcon::Normal, QIcon::Off);
          play.addPixmap(QPixmap(":icon/play.png"), QIcon::Selected, QIcon::On);
          play.addPixmap(QPixmap(":icon/play.png"), QIcon::Selected, QIcon::Off);
          play.addPixmap(QPixmap(":icon/play.png"), QIcon::Active, QIcon::On);
          play.addPixmap(QPixmap(":icon/play.png"), QIcon::Active, QIcon::Off);
          

          None of these alter the behaviour at all unfortunately.

          A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          @skebanga Something else is going on then. How are using that QAction? Can we see the code where it is added to the bar? What happens when you click it? Does it get "toggled"?

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          S 1 Reply Last reply
          0
          • A ambershark

            @skebanga Something else is going on then. How are using that QAction? Can we see the code where it is added to the bar? What happens when you click it? Does it get "toggled"?

            S Offline
            S Offline
            skebanga
            wrote on last edited by
            #5

            @ambershark I have created a minimal example replicating the behaviour I see

            <!DOCTYPE RCC><RCC version="1.0">
              <qresource prefix="icon">
                <file>play.png</file>
              </qresource>
            </RCC>
            
            #include <QApplication>
            #include <QMainWindow>
            #include <QMenuBar>
            
            void initIcons()
            {
            	Q_INIT_RESOURCE(view);
            }
            
            int main(int argc, char** argv)
            {
            	QApplication* app = new QApplication(argc, argv);
            	QMainWindow*  window = new QMainWindow();
            
            	QMenuBar* menu = new QMenuBar();
            	window->setMenuBar(menu);
            
            	QIcon play;
            	play.addFile(":icon/play.png", QSize(), QIcon::Normal);
            	play.addFile(":icon/play.png", QSize(), QIcon::Active);
            
            	QAction* action = new QAction(play, "", nullptr);
            	menu->addAction(action);
            
            	window->show();
            	return app->exec();
            }
            

            Here is the icon unclicked

            alt text

            Here is the icon clicked

            alt text

            A 2 Replies Last reply
            0
            • S skebanga

              @ambershark I have created a minimal example replicating the behaviour I see

              <!DOCTYPE RCC><RCC version="1.0">
                <qresource prefix="icon">
                  <file>play.png</file>
                </qresource>
              </RCC>
              
              #include <QApplication>
              #include <QMainWindow>
              #include <QMenuBar>
              
              void initIcons()
              {
              	Q_INIT_RESOURCE(view);
              }
              
              int main(int argc, char** argv)
              {
              	QApplication* app = new QApplication(argc, argv);
              	QMainWindow*  window = new QMainWindow();
              
              	QMenuBar* menu = new QMenuBar();
              	window->setMenuBar(menu);
              
              	QIcon play;
              	play.addFile(":icon/play.png", QSize(), QIcon::Normal);
              	play.addFile(":icon/play.png", QSize(), QIcon::Active);
              
              	QAction* action = new QAction(play, "", nullptr);
              	menu->addAction(action);
              
              	window->show();
              	return app->exec();
              }
              

              Here is the icon unclicked

              alt text

              Here is the icon clicked

              alt text

              A Offline
              A Offline
              ambershark
              wrote on last edited by
              #6

              @skebanga Cool, let me mess with this real quick and see if I can duplicate it and if so if I can find the problem. I'll let you know either later tonight or sometime tomorrow.

              My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

              1 Reply Last reply
              0
              • S skebanga

                @ambershark I have created a minimal example replicating the behaviour I see

                <!DOCTYPE RCC><RCC version="1.0">
                  <qresource prefix="icon">
                    <file>play.png</file>
                  </qresource>
                </RCC>
                
                #include <QApplication>
                #include <QMainWindow>
                #include <QMenuBar>
                
                void initIcons()
                {
                	Q_INIT_RESOURCE(view);
                }
                
                int main(int argc, char** argv)
                {
                	QApplication* app = new QApplication(argc, argv);
                	QMainWindow*  window = new QMainWindow();
                
                	QMenuBar* menu = new QMenuBar();
                	window->setMenuBar(menu);
                
                	QIcon play;
                	play.addFile(":icon/play.png", QSize(), QIcon::Normal);
                	play.addFile(":icon/play.png", QSize(), QIcon::Active);
                
                	QAction* action = new QAction(play, "", nullptr);
                	menu->addAction(action);
                
                	window->show();
                	return app->exec();
                }
                

                Here is the icon unclicked

                alt text

                Here is the icon clicked

                alt text

                A Offline
                A Offline
                ambershark
                wrote on last edited by
                #7

                @skebanga Ok so I played with it.. This seems like a bug in QMenuBar (maybe specific to linux).

                Here's what I found... If you add text to your action, i.e. new QAction(play, "play") then when you click the play icon it will change to the text "play" as you hold the button down, highlighted with your selection color (in your case blackish gray, in mine blue).

                So the reason you are seeing the blank is because it is showing text only and highlighted... so when your text is "" it basically shows a highlighted blank text/space.

                That feels like it shouldn't be happening. It shouldn't be changing to the text mode for the highlight, it should highlight the icon as the QToolBar does.

                So you could do a few things:

                1. Override QMenuBar and handle that click highlight yourself.
                2. Switch to a QToolBar - the highlighting of icons works properly on these, see the Qt example widgets/mainwindows/application.
                3. Wait for a fix from Qt (assuming you report the bug and it is confirmed as a bug).

                I don't see any function in QAction or QMenuBar that could change this behavior. It makes me think it was not intentional... Or I just missed the function that fixes it. ;)

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                S 1 Reply Last reply
                0
                • A ambershark

                  @skebanga Ok so I played with it.. This seems like a bug in QMenuBar (maybe specific to linux).

                  Here's what I found... If you add text to your action, i.e. new QAction(play, "play") then when you click the play icon it will change to the text "play" as you hold the button down, highlighted with your selection color (in your case blackish gray, in mine blue).

                  So the reason you are seeing the blank is because it is showing text only and highlighted... so when your text is "" it basically shows a highlighted blank text/space.

                  That feels like it shouldn't be happening. It shouldn't be changing to the text mode for the highlight, it should highlight the icon as the QToolBar does.

                  So you could do a few things:

                  1. Override QMenuBar and handle that click highlight yourself.
                  2. Switch to a QToolBar - the highlighting of icons works properly on these, see the Qt example widgets/mainwindows/application.
                  3. Wait for a fix from Qt (assuming you report the bug and it is confirmed as a bug).

                  I don't see any function in QAction or QMenuBar that could change this behavior. It makes me think it was not intentional... Or I just missed the function that fixes it. ;)

                  S Offline
                  S Offline
                  skebanga
                  wrote on last edited by
                  #8

                  @ambershark Thanks for giving it a try! I've submitted a bug report

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

                    I just realised one thing, you seem to be using Ubuntu. Are you using the Qt version from your distribution ? If not, then please try with it before opening a report. It might be related to their customisation.

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

                    S 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      I just realised one thing, you seem to be using Ubuntu. Are you using the Qt version from your distribution ? If not, then please try with it before opening a report. It might be related to their customisation.

                      S Offline
                      S Offline
                      skebanga
                      wrote on last edited by
                      #10

                      @SGaist I'm using Qt 5.5 downloaded from the Qt website. Is this not the correct way to obtain Qt?

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

                        This is one way and usually the only one to get the latest version when it comes out.

                        However, most Linux distribution provide Qt and its development environment because it's a widely used framework and there are usually several system tools using it. This is not only valid for KDE which is based on Qt but also for example for WireShark and other tools which front end are written using Qt.

                        Thus depending on your target customer/user/etc. you might also want to use your distribution provided Qt to build packages that fits in. In the Ubuntu case, IIRC, there where some customisation done to integrate with their Window Manager.

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

                        S 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          This is one way and usually the only one to get the latest version when it comes out.

                          However, most Linux distribution provide Qt and its development environment because it's a widely used framework and there are usually several system tools using it. This is not only valid for KDE which is based on Qt but also for example for WireShark and other tools which front end are written using Qt.

                          Thus depending on your target customer/user/etc. you might also want to use your distribution provided Qt to build packages that fits in. In the Ubuntu case, IIRC, there where some customisation done to integrate with their Window Manager.

                          S Offline
                          S Offline
                          skebanga
                          wrote on last edited by
                          #12

                          @SGaist Ok, thanks! I'll try it out

                          A 1 Reply Last reply
                          0
                          • S skebanga

                            @SGaist Ok, thanks! I'll try it out

                            A Offline
                            A Offline
                            ambershark
                            wrote on last edited by
                            #13

                            @skebanga If it helps I was testing on Arch Linux running without KDE and no preinstalled system wide Qt.

                            So it's probably not a Ubuntu thing. However like @SGaist I thought that originally since Ubuntu does OSX style stuff with menu bars. I figured that was the issue but it turned out not to be.

                            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                            S 1 Reply Last reply
                            0
                            • A ambershark

                              @skebanga If it helps I was testing on Arch Linux running without KDE and no preinstalled system wide Qt.

                              So it's probably not a Ubuntu thing. However like @SGaist I thought that originally since Ubuntu does OSX style stuff with menu bars. I figured that was the issue but it turned out not to be.

                              S Offline
                              S Offline
                              skebanga
                              wrote on last edited by
                              #14

                              @ambershark Ok, thanks for confirming that

                              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