View Full Version : Styling a Single Item on a QMenuBar

9th November 2011, 22:56
I already know what you're going to say, so please don't chastise me about user interface consistency/standards. I know this isn't a good technique, but I'm porting an application that was done this way (and no, it wasn't my idea). I'm looking for technical answers about how to do this, not reasons why I shouldn't do it. :)

I have a situation where an item on the menu bar is not selectable and doesn't produce a menu. In the existing application, this item is updated to show different brief, status information related to the information currently on display. The background color of the item changes along with the text of the menu item.

In Qt, creating the empty menu isn't a problem, nor is changing the text, but attempting to style the menu item with setStyleSheet doesn't seem to work, either programmatically or by experimenting manually in QtCreator. My best guess is that the style of the items on the menu bar is controlled by the menu bar and attempts to override that style are ignored. The code line I'm using is below; I've tried both the background and foreground color. Neither seems to have any effect. The style string is generated by the style editor in QtCreator.

this->ui->menuContext->setStyleSheet ("background-color: rgb(255, 85, 0);\ncolor: rgb(0, 170, 255)");

If so, that's fine, and I can go to my team and say "hey, we can't do it this way anymore" and I'm fine with that. If not, and I'm doing something wrong technically, at the very least, it would be nice to understand.

The old application is Motif-based, and this would be the first thing I've come across that I can't do in Qt. I'm not saying that's a bad thing, but I am a bit surprised.


9th November 2011, 23:37
Maybe you can create a subclass of QMenu, reimplement paintEvent, do all custom painting there and set it to menu bar via QMenuBar::addMenu ?

10th November 2011, 02:42
Thanks for the suggestion, but after discussing with a colleague, I've decided to move these items onto a toolbar where they more properly belong.


10th November 2011, 08:04
I would try to make a custom widget. Widget will contain a menubar and a custom widget(label?) in a horizontal layout. Add this widget in place of your menubar.