c++ - 非事件窗口中的 QMenuBar 项目样式

标签 c++ qt windows-7 qt4

当窗口失去焦点时,如何禁止更改 QMenuBar 中的菜单项外观?

现在,当窗口获得焦点时,菜单项清晰可见,但当它失去焦点时,项目是灰色的,看起来像禁用了。我希望它们始终看起来正常。

我的平台是 Windows7 上的 Qt4。

事件窗口和非事件窗口菜单项的一些简单截图:

menu item normal menu item on inactive window

最佳答案

使用 QStylesheets 并利用 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

如果你想忽略样式表的另一个选项,你可以试试调色板。

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/

相关文章:

c++ - 为什么这两个模板函数的输出不同?

c++ - 程序得到 "Expression: string subscript out of range"

Qt - 键盘布局

c++ - 自动更新 QLabel 文本

windows-7 - Windows 7 上的 Cygwin 权限

php - 寻找适用于 Windows 7 的 OAuth DLL(需要从 WAMP 服务器上的 PHP 使用)

c++ - 在运行它之前,如何通过 lua C API 为一段 lua 代码设置环境表?

c++ - 如何使用正交投影将 3d 渲染图像(透视投影)绘制回另一个视口(viewport)。同时使用多个视口(viewport)和 OpenGL

c++ - 如何解决 Qt Creator 变量 -“<not accessible>” 行为 (Opencv)

windows - 如何以非管理员身份在 Windows 7 上安装 yarn?