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

QIcon in QMenu does not change image with mouse hover or click



  • I am trying to setup a QIcon for a QMenu that reflects normal, pressed, and disabled states but the image doesn't seem to change when I mouse hover/click over the icon. I have added 3 icons to my resources.qrc file reflecting normal, active, and disabled states.

    Here are the macro definitions:

    #define IMG_SETTINGS_NORMAL     ":/Images/icon-menu.png"
    #define IMG_SETTINGS_ACTIVE     ":/Images/icon-menu-pressed.png"
    #define IMG_SETTINGS_DISABLED   ":/Images/icon-menu-disabled.png"
    

    Here is my code:

        m_pMenu->addAction(m_pAction1);
        m_pMenu->addAction(m_pAction2);
    
        QIcon settingsIcon;
        settingsIcon.addFile(IMG_SETTINGS_NORMAL,   QSize(), QIcon::Normal);
        settingsIcon.addFile(IMG_SETTINGS_ACTIVE,   QSize(), QIcon::Active);
        settingsIcon.addFile(IMG_SETTINGS_DISABLED, QSize(), QIcon::Disabled);
        m_pMenu->setIcon(settingsIcon);
    
        m_pMenuBar->setStyleSheet(
            R"style(
              QMenuBar::item:pressed
              {
                  background: transparent;
              }
            )style");
    
        m_pMenuBar->addMenu(m_pMenu);
        m_pMenu->setLayoutDirection(Qt::LeftToRight);
        m_pMenuBar->setNativeMenuBar(false);
    

    Please let me know any other information I can provide. Any help would be most appreciated.


  • Lifetime Qt Champion

    A QMenu does not show anything - I would try to set the icon to the actions



  • @Christian-Ehrlicher
    Appreciate your response.
    So I do see the QIcon image for normal state: "icon-menu.png" I just don't see it showing "icon-menu-pressed.png" on mouse hover or click.

    What code updates do you suggest I make?



  • @Christian-Ehrlicher
    Also if I have multiple actions for a QMenu dropdown do I need to add the QIcon to all the QActions? Won't this add an icon to the drop down list of actions? I just want the QIcon for the QMenu.



  • @anshah
    You wish to show these icons against the action-items you place on your menu. https://doc.qt.io/qt-5/qaction.html#icon-prop rather than https://doc.qt.io/qt-5/qmenu.html#icon-prop. Does that not do what you want?



  • @JonB
    I just want to show these icons on the QMenu itself not the QActions in the dropdown. The QActions do not have any icons next to them. I just want the icon to change states based on mouse hover/clicks or disabled.


  • Lifetime Qt Champion

    A QMenu can't draw different icons based on the states you want. It will only use the normal state afaics. And this is working fine for me (tried it in qt designer).



  • @Christian-Ehrlicher
    So when you say actions would this work:

        QIcon settingsIcon;
        settingsIcon.addFile(IMG_SETTINGS_NORMAL,   QSize(), QIcon::Normal);
        settingsIcon.addFile(IMG_SETTINGS_ACTIVE,   QSize(), QIcon::Active);
        settingsIcon.addFile(IMG_SETTINGS_DISABLED, QSize(), QIcon::Disabled);
    
        QAction* action = new QAction(settingsIcon, "", nullptr);
        m_pMenu->addAction(action);
        m_pMenu->setIcon(settingsIcon);
    

    Update to code above.



  • @anshah
    We are talking about action->setIcon(settingsIcon). EDIT OIC, you have new QAction(settingsIcon, "", nullptr);. Yes, try that.



  • @JonB
    I tried that and what I'm seeing is the icon displays in the drop down menu next to null text ("").

    Arrrgh I'm almost thinking I'm going to have to abandon addFile and QIcon::Mode and catch the aboutToShow and aboutToHide QMenu signals and do m_pMenu->setIcon.


Log in to reply