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

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



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


  • Lifetime Qt Champion

    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



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


  • Moderators

    @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"?



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


  • Moderators

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


  • Moderators

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



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    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.



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


  • Moderators

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



  • @ambershark Ok, thanks for confirming that


Log in to reply