PDA

View Full Version : Weird QMenu behavior on Mac



munna
14th January 2009, 13:07
Hi!

When I set palette on QMenu it doesn't seem to be working. The following is the code



Selector_Menu::Selector_Menu(QWidget *parent) : QMenu(parent)
{
setMinimumWidth(52);

setAutoFillBackground(true);

QPalette p = palette();
p.setColor(QPalette::Window, QColor(5, 5, 5));
setPalette(p);
}


I add actions to this menu from the function where I create this object. Something like this



menu = new Selector_Menu(this);
currentAction = menu->addAction("A");
menu->addSeparator();
menu->addAction("B");
menu->addSeparator();
menu->addAction("C");


Output looks like the Picture1 attached

When I use stylesheets I get the output which looks like the Picture2 attached



RMS_Output_Assignment_Selector_Menu::RMS_Output_As signment_Selector_Menu(QWidget *parent) : QMenu(parent)
{
setMinimumWidth(52);

setAutoFillBackground(true);

QString menu_ss = "QMenu { color: white; background-color: black; }" +
QString("QMenu::item:selected { color: grey; background-color: black; }");
setStyleSheet(menu_ss);
}


Any idea on why is the background not being set properly in the menu. I am using Qt 4.3.1

Thanks a lot for the help

tinsuke
14th January 2009, 15:18
Hey Munna!

I've already implemented applications both StyleSheet and Palette based for Mac OS X.

The truth is: they're both (speacially the Palette approach) buggy on (specially, again) Mac.

Up to Qt 4.4.3, some QWidgets properties (including palette) propagation has erronous behaviours. This issue seem to be addressed on Qt 4.5. But, as a workaround, you could implement some function like this:

void applyPaletteTo(const QPalette &palette, QWidget *widget)
{
widget->setPalette(palette);
foreach (QObject *object, widget->children())
if (QWidget *child = (object->isWidgetType() ? dynamic_cast<QWidget *>(object) : NULL))
applyPaletteTo(palette, child);
}

and call

applyPaletteTo(yourCustomPalette, yourCustomMenu);

Note that, for this to have effect, it must be done AFTER the widget construction (not the constructor, but the whole 'addSomething' stuff).

Now for your StyleSheet problem, try the following:

QString menu_ss = "QMenu { color: white; background-color: black; }"
"QMenu::item { background-color: black; }"
"QMenu::item:selected { color: grey; background-color: black; }";


Best regards,
tinsukE