当窗口失去焦点时,如何禁止更改 QMenuBar 中的菜单项外观?
现在,当窗口获得焦点时,菜单项清晰可见,但当它失去焦点时,项目是灰色的,看起来像禁用了。我希望它们始终看起来正常。
我的平台是 Windows7 上的 Qt4。
事件窗口和非事件窗口菜单项的一些简单截图:
最佳答案
使用 QStylesheets 并利用 QMenuItems 的状态。
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
如果你想忽略样式表的另一个选项,你可以试试调色板。
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.
因此您应该能够为您的 QMenuItem 获取调色板的拷贝,将事件调色板复制到非事件调色板中,然后在您的 QMenuItem 上调用 setPalette。 Tada,现在它看起来总是很活跃。
希望对您有所帮助。
关于c++ - 非事件窗口中的 QMenuBar 项目样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35149720/