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. QIcon in QMenu does not change image with mouse hover or click
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 2.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.
  • A Offline
    A Offline
    anshah
    wrote on 30 May 2019, 17:27 last edited by anshah
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 30 May 2019, 17:36 last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • A Offline
        A Offline
        anshah
        wrote on 30 May 2019, 17:40 last edited by anshah
        #3

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          anshah
          wrote on 30 May 2019, 17:51 last edited by anshah
          #4

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

          J 1 Reply Last reply 30 May 2019, 18:06
          0
          • A anshah
            30 May 2019, 17:51

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

            J Offline
            J Offline
            JonB
            wrote on 30 May 2019, 18:06 last edited by
            #5

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

            1 Reply Last reply
            1
            • A Offline
              A Offline
              anshah
              wrote on 30 May 2019, 18:09 last edited by anshah
              #6

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

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 30 May 2019, 18:19 last edited by
                #7

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

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                A 1 Reply Last reply 30 May 2019, 18:49
                0
                • C Christian Ehrlicher
                  30 May 2019, 18:19

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

                  A Offline
                  A Offline
                  anshah
                  wrote on 30 May 2019, 18:49 last edited by anshah
                  #8

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

                  J 1 Reply Last reply 30 May 2019, 20:04
                  0
                  • A anshah
                    30 May 2019, 18:49

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

                    J Offline
                    J Offline
                    JonB
                    wrote on 30 May 2019, 20:04 last edited by JonB
                    #9

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

                    A 1 Reply Last reply 30 May 2019, 20:09
                    0
                    • J JonB
                      30 May 2019, 20:04

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

                      A Offline
                      A Offline
                      anshah
                      wrote on 30 May 2019, 20:09 last edited by anshah
                      #10

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

                      1 Reply Last reply
                      0

                      1/10

                      30 May 2019, 17:27

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved