开发者

How to style a submenu item in Qt?

I would like to style a menu item that has a submenu differently, just to make this submenu standout. But the style is being applied to the menu items in the submenu rather than styling the submenu item alone.

Menu1
Menu2 -> menu21
          menu22
          menu23
Menu3

I wanted to style only menu2, but Qt is styling menu21, 开发者_如何学Gomenu22 and menu23.

Tried all these:

subMenu->setStyleSheet("QMenu{ font: bold }");
subMenu->setStyleSheet("QMenu::item{ font: bold }");
subMenu->setStyleSheet("QMenu::item#subMenu{ font : bold }");

Dynamic properties also did not help, setting a style on the action associated with subMenu is crashing Qt! :(

Thanks for your time,

Gowtham


See the documentation:

  • Qt Style Sheets Reference - QMenu
  • Qt Style Sheets Examples - Customizing QMenu

I haven't tested this, but in theory, you may be able to use QMenu::item with either a Property Selector or a ID Selector (see Qt Style Sheet Syntax - Selectors). For example:

QMenu::item[text="Menu2 text"]

or

QMenu::item#menu2Id

though it may be difficult to get or set the ID.

Of course, it may turn out that it's impossible to set individual styles for QMenu entries outside of the cases shown in the Qt examples.


I would put this in a comment, but I have no right to add comments :-(

The links provided by Daggerstab did not work for me. Here is another one to an example for customizing QMenu

  • Example for customizing QMenu
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜