PDA

View Full Version : Two icons for each item in QMenu?



ultr
1st March 2009, 00:38
Hello,

Is there a way to set two separate icons for each QMenu item?

So that menu would look like this:
[ ] [ ] Item1
[ ] [ ] Item2
[ ] [ ] Item3
[ ] [ ] Item4


Thank you in advance.

wysota
1st March 2009, 00:44
No, I don't think so. But you can compose an icon from two images.

Brandybuck
1st March 2009, 00:45
You can't do that "out of the box" with QMenu. But you could write your own QMenu subclass that supports two icons.

ultr
1st March 2009, 01:02
@wysota

I've checked this already. When I set item's icon to 16x32 px image, it will be scaled down to 8x16.


@Brandybuck

I looked through qmenu.cpp, but I cannot find any place where the icon and label are drawn or inserted into layout :/

Brandybuck
1st March 2009, 05:26
Menu items are normally drawn with QStyle, so you would have to do your own drawing of it. I think Wysota's suggestion will be easier.

ultr
1st March 2009, 17:54
Well, what wysota said is quite a good idea. But the icons are getting scaled down.

I tried to change their appearance with ::setStyleSheet() both for qApp and the menu. But no luck.
I tried setting style for QMenu::item::icon, QMenu::icon and some more combinations.

Did I do something wrong, or there is no way to style the icon to have 32px width?

drhex
1st March 2009, 18:33
Try calling setStyle() on your menu, with a style defined like this:


class myStyle: public QPlastiqueStyle
{
Q_OBJECT
public:
int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0 ) const
{
if (metric == QStyle::PM_SmallIconSize)
return 32; // default is 16
else
return QPlastiqueStyle::pixelMetric(metric, option, widget);
}
};

ultr
2nd March 2009, 13:08
@drhex

This works fine, however it changes the style of the menu making the application looking bad.

It seems icon size depends on the style and how it decides to draw it. And only QPlastiqueStyle respects PM_SmallIconSize value.

ultr
9th March 2009, 09:02
I have created WideIconsMenu class that should solve this problem.

It uses ProxyStyle (http://wiki.qtcentre.org/index.php?title=Proxy_Style) for Qt4 from QtCentre's wiki, and similar class for Qt3. Both Qt3 and Qt4 are supported.

Multiple icons cannot be used, however WideIconsMenu draws the icons on its own, so icons of any width are supported. Everything you have to do is to compose an icon from several images.
See included examples for more details.

The code is available under GPLv3 license.

If you find a bug, or have a suggestions, please send me an email.

ultr
15th December 2010, 15:13
Since Qt 4.6 and above provides QProxyStyle class, the ProxyStyle code is no longer needed.

The attached archive contains 3 versions of WideIconsMenu:
- for Qt 3 (no changes)
- for Qt 4.x (no changes)
- for Qt 4.6+ (new)