Search code examples
c++qtwindows-7qt4

QMenuBar items style in inactive window


How to disable changing menuItems look in QMenuBar when window loses focus?

Now, when window has focus, menu items are clearly visible, but when it loses focus, items are gray, looks like disabled. I want them to look normal all the time.

My platform is Qt4 on Windows7.

Some simple screenshot of menu item on active and inactive window:

menu item normal menu item on inactive window


Solution

  • Use QStylesheets and leverage the states of your QMenuItems.

    http://www.qtcentre.org/threads/37560-QPushButton-different-stylesheets-for-focus-pressed-released-combinations

    QPushButton{ background-color: blue; }
    QPushButton:disabled{ background-color: yellow; }
    QPushButton:pressed{ background-color: orange; }
    QPushButton:focus:pressed{ background-color: black; }
    QPushButton:focus{ background-color: green; }
    QPushButton:hover{ background-color: red; }
    QPushButton:checked{ background-color: pink; }
    

    http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qmenubar

    The other option if you want to ignore stylesheets, you could try the palette.

    http://doc.qt.io/qt-5/qpalette.html#details

    The color groups:

    • The Active group is used for the window that has keyboard focus.
    • The Inactive group is used for other windows.
    • The Disabled group is used for widgets (not windows) that are disabled for some reason.

    So you should be able to get the copy of the palette for your QMenuItem, copy the active palette into the inactive palette, and then call setPalette on your QMenuItem. Tada, now it always looks active.

    Hope that helps.