QMenu styling issue
I'm trying to create a bit of space on the left side of a QMenu in order to put some graphical elements (an icon and a line) left of the menu items. I figured I should be able to do that using Qt stylesheets, as QMenu "supports the Box model" according to the documentation. So, I wend ahead and in my subclass of QMenu did:
While that creates the needed space on the left, it also creates the same space on the right of the items. I tried to get rid of that space, but I have failed so far. Can anyone here come up with a trick to create some space left of the menu items but not to the right of them?
mairesse last edited by
I might be wrong, because I'm new in Qt and stuff but usually when I use QMenu (as context menus) I add actions to them, right?
I just create the action with the desired icon I want for it and then I add it to the QMenu and it places the icon just in the left of action's text.
[code]QAction *action = new QAction(QIcon(":/resources/myActionIcon.png"), "Action with icon", this);[/code]
Sorry if I misunderstood.
No, this is explicitly not what I mean. Sorry if I was unclear. I know I can add icons to menu items, but I want to add an icon to the menu itself, like this:
!https://dl.dropbox.com/u/16442531/menumockup.png(Mockup of customized menu)!
The icon I'm talking about is the one on the left. What I end up with so far is this:
!https://dl.dropbox.com/u/16442531/menuscreenshot.png(Screenshot of menu)!
Notice the excess space on the right of the menu items, as well as in the menu items themselves.
KA51O last edited by
Have you tried to set the right padding space explicitly to zero ?
Yes, I tried, and that yielded no difference.
Edit: What does work (well, almost), is to reimplement sizeHint like this:
QSize IconMenu::sizeHint() const
QSize hint = QMenu::sizeHint();
hint.setWidth(hint.width() - 80);
It looks ok, but for the fact that now the right side of the item highlights is not correctly rendered. Instead, it is just cut-off. It is a big improvement over what I had, but I'd considder this a hack, not a solution.
Why are you decreasing the width sizeHint by 80 instead of 40 (isn't it 40 pixels too wide on the right? Probably I'm missing something).
Also not a very nice solution, but you could try to set a negative padding (or margin) to the right, so for example:
(Or margin, don't know what would work better).
I don't know if Qt supports negative margins/paddings).
Because with 80px, it also gets rid of the excess space inside the items. Still, I am not happy with it at all.
I'll experiment with setting a negative right padding and/or margin.
Oh, I didn't read that part, sorry :)
Just one more question out of curiosity: Is it right that you end up with one times 40px padding on the left and two times 40px on the right (one within the item, one outside)? Or do you draw the individual item icons yourself and thus you have two times 40px on the left as well?
No, I end up with the right amount of padding on the left, but on the right, I have both padding outside as well as inside the items. Perhaps I should mention that I also use a proxy style on the menu. This proxy style gives a larger value for the small icons (the icons next to the items), because I need them to be bigger than the normal 16x16 or so.
The only icon I draw manually it the bigger one on the top-left. The vertical line I also render myself. The rest of the dialog is rendered by Qt by calling QMenu::paintEvent(QPaintEvent* e) from my own implementation of that method.
Hm, I just played around a little with the style sheet and menus, and I as far as I can see, Qt just completly ignores the right padding and takes the left one instead.
So while @helpMenu->setStyleSheet("padding-right:50px");@
has no effect,
has the effect that you described: It adds to both sides.
Maybe it indeed is a bug?
I have filed a "bugreport":https://bugreports.qt-project.org/browse/QTBUG-26799 for this issue.