PDA

View Full Version : QMenu always displays icons aty 16x16 px



ghassett
21st May 2009, 16:30
Hi - I am using QMenu, and I am using a large font in the menu. I am providing 24x24 pixel icons to the QMenu when I add actions to it, but when the menu renders, my icons have been "downsized" to 16x16 pixels, and look too small next to my larger menu text.

I could not find out how to set the icon size -- I tried setting the style of the menu to this style sheet, but no joy. Any help is much appreciated!

QMenu::item { padding: 2px 32px 2px 20px; }
QMenu::item:selected { background-color: blue; color: white }
QMenu::icon { width:24px; height:24px; }
QMenu::indicator { width:24px; height:24px; }

- Greg

aamer4yu
21st May 2009, 17:06
I guess you will need to provide a new style. You can derive it from the style classes in Qt.

ghassett
21st May 2009, 18:33
Thank you for the pointer... here's what I tried.

I derived my own style from QPlastiqueStyle and overrode the pixelMetric() function. QPlastiqueStyle calls its pixelMetric() method when it is drawing a menu item, to find out the pixel metrics for PM_SmallIconSize. My overridden class re-implements the virtual pixelMetrics like this:


int MyStyle::pixelMetric (PixelMetric metric, const QStyleOption* option, const QWidget* widget)
{
qDebug () << "my menu style pixelMetric called!";

if (metric == PM_SmallIconSize)
{
return 24;
}
else
{
return QPlastiqueStyle::pixelMetric (metric, option, widget);
}
}

Next, I instantiate an object of type MyStyle and pass it to my QMenu's setStyle method:


MyStyle myStyle;
QMenu menu;

menu.setStyle (&myStyle);

However, when I pop up that menu, my pixelMetric function (in my derived class) never gets called -- and the icon is still displayed at 16x16. Any suggestions?

Thanks


Greg

ghassett
21st May 2009, 18:41
Joy!

I was forgetting the 'const' at the end of my method override for the pixelMetric function. So I wasn't really overriding the function in the base class, due to the signature mismatch (base class's method was const, mine wasn't).

Making my pixelMetric function const solved the problem.

Thanks for your help!

:)