View Full Version : Menu palette question

21st February 2007, 18:47

First I am new to the qcentre forums and I would like to introduce myself. My name is Susan and am fairly new to Qt4 (I've experience with Qt3); I have been working with it for the last couple of months.

I am working on a prototype and have to apply custom colors to almost everything. This is really easy to do with ColorRoles, etc. in widget palettes. But I have been stumped when trying to apply a color to a popup or pulldown menu.

For example, the menu that is displayed when a combo box is activated needs a specific color. I haven't been able to figure out how to do that. When I access the backgroundRole or foregroundRole of the QAbstractItemView (via the ::view() method), it is Window and WindowText respectively, and when set the view's palette (setting these roles to different colors) I get no change in color!

Any hints on what needs to be done for menus?

Much appreciated in advance,
Susan Macchia

21st February 2007, 18:54
Hi, welcome on board! ;)

QPalette p = comboBox->view()->palette();
p.setColor(QPalette::Base, Qt::red);

21st February 2007, 20:12
Thank you. Unfortunately that didn't work. Neither did:

QAbstractItemView *v = combo->view();
QPalette p;
p.setColor(QPalette::Base, Qt::red);
p.setColor(QPalette::Text, Qt::white);

I see a brief flash of the red only on the selected item. Then it goes back to the default. The menu itself (apart from the selected item) is not affected by the color choice.

I tried ::setColor with all the roles to no avail...

Any other pointers?

Thanks much.

21st February 2007, 21:21
Oh, you are running GNOME? Seems like it indeed doesn't work with the cleanlooks style.. Stylesheets introduced in 4.2 usually are a solution to palette problems, but even this doesn't work:

comboBox->setStyleSheet("QAbstractItemView { background: yellow }");

Feels like a bug to me.. :(

21st February 2007, 22:13
Yes I am running under gnome. The final product will be running without a window manager. I'll see how it works under KDE and study style sheets. I'll report back with my findings.

21st February 2007, 22:23
By the way, you can preview it with another style by passing a cmd line parameter:

$ ./yourapp -style plastique

21st February 2007, 22:39
Yes I know, though that may not impact the settings under Gnome. I'll experiment a bit more and Thanks for all the help on this!

22nd February 2007, 15:33
Under gnome, using the style sheet works; I was able to get it to work programatically using the ::setStyleSheet method and by reading an external .qss file.

So there is a bug using the palette. I also tried it under ICWM and found the same bug, so it may not be related to the desktop or window manager. Nevertheless, I'll submit a bug to Trolltech on the palette problem.

Thanks for the pointers, they really helped!

I am finding that I'll probably need to use a combination of style sheets and palettes because, for example, when you set the color of a QPushButton using the style sheet, you lose the Plastique look & feel (haven't tried it running under another L/F). But it works fine for QToolButton & QComboBox. I also noticed that I wasn't able to set the background color of tables or lists in style sheets either. But I'll continue working with it.

26th February 2007, 20:59
So now I see another issue using the stylesheet for the combo box instead of QPalette. In the plastique style there is a "check" next to the selected item and it shouldn't be there in plastique. How would I make sure the L/F of the widget adheres to the platform style?