PDA

View Full Version : Change background color of one menubar item eg Help in blue



maulik_887
4th April 2012, 07:36
Hello All,

I'm new to QT and I'm stuck at one point.
I have some of my menu bar items in right side (using this code http://qt-project.org/faq/answer/how_can_i_draw_one_of_my_menu_items_e.g_help_to_th e_right_in_the_menubar). And I want the background color of those menu bar items will be blue/some icon.
Is this possible in QT ? If so then could anyone provide me a way to do so ?

Thanks in advance.

--Maulik Patel

mentalmushroom
4th April 2012, 15:05
you can try something like this:


#include <QApplication>
#include <QMainWindow>
#include <QWidgetAction>
#include <QMenuBar>
#include <QMenu>
#include <QLabel>
#include <QStyle>
#include <QDebug>

int
main (int argc,
char **argv)
{
QApplication app(argc, argv);
app.setStyleSheet("QLabel#label1 { background-color: blue }" \
"QLabel#label2 { background-color: yellow }");

QMainWindow win;

QLabel *label1 = new QLabel("Action1");
label1->setObjectName("label1");
QWidgetAction action1(&win);
action1.setDefaultWidget(label1);

QLabel *label2 = new QLabel("Action2");
label2->setObjectName("label2");
QWidgetAction action2(&win);
action2.setDefaultWidget(label2);

QMenu menu("Edit");
menu.addAction(&action1);
menu.addAction(&action2);

win.menuBar()->addMenu(&menu);
win.show();

return app.exec();
}

maulik_887
5th April 2012, 09:23
Thanks for the reply. Sorry, but it didnt help.

Let me describe my problem again. I want to have different colored "Menu Bar Items" not "Menu Item".
I have some Menu bar items like File, Edit etc in left site.. those will be in default color. And Some in right side say Help.. and that HELP itself should be in blue color(or have some BG image/style) not it's items(help->show help, Help->About).

Something like this..
7564

Is this possible ?

mentalmushroom
5th April 2012, 10:49
This seems to be a hole in Qt. I didn't find a way to do that. Maybe it is possible if you reimplement QMenuBar or QStyle and provide your custom rendering. As a workaround I can propose you to use QToolBar instead of QMenuBar, where you can add labels or button via QWidgetAction and style them like in the sample above. With that approach you will have to display menu manually, but that should not be difficult.

maulik_887
6th April 2012, 06:03
Okay.. Will try to implement it as you suggested.

Spitfire
9th April 2012, 13:41
AFAIK you can change background color of all items in the menu bar but not some/specific items

QMenuBar:item { background-color: rgb(255, 37, 186); }


To do what you want you would need to inherit from menu bar and paint it the way you want.