Issue
I'm trying to change the styling and/or font of the label/text on the QMenu, without affecting it's children. I'm doing this in Python with PySide (which works just like Qt).
I've tried:
menu = QtGui.QMenu()
f = menu.font()
f.setBold(True)
menu.setFont(f)
And
menu = QtGui.QMenu()
menu.setStyleSheet("QMenu{font-weight: bold;}")
Both of these would not change the label of the menu itself, yet will do it on all of its children.
I would prefer to set the styling directly on the QMenu (or another class if it acts similarly and makes it possible) instead of applying a stylesheet on its parent.
Goals
The idea is that I have a menu with a variety of sub-menus (which are somewhat dynamic based on folders on a server) of which some need to be Bold and some Italic. Therefore I would like to add these sub QMenu's dynamically and style them accordingly.
Solution
You can achieve it easy. To understand the logic:
QMenu
- is a widget which displays items- When you add
QMenu
to anotherQMenu
QMenu::menuAction
is placed in a base menu. - If you want to customize how your submenu looks when it's added to a base menu, you need to customize the
menuAction
.
So to make you menu bold you just need to set a bold font to menuAction
of this menu.
Here is a simple working example:
QMenu m; //base menu
QMenu sub; //sub menu
sub.setTitle("subMenu");
QAction* a1 = new QAction("act1", &m);
QAction* a2 = new QAction("act2", &m);
QAction* a3 = new QAction("act3", &m);
// set a bold font for a sub menu item
QFont f = sub.menuAction()->font();
f.setBold(true);
sub.menuAction()->setFont(f);
// add an action to the sub menu
sub.addAction(a3);
// add two actions and the sub menu to the base menu
m.addAction(a1);
m.addMenu(&sub);
m.addAction(a2);
// show the base menu
m.exec(QCursor::pos());
Answered By - Ezee
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.