PDA

View Full Version : How to change Icon size in QMenu?



alex chpenst
2nd September 2008, 23:25
Hi, I can't find how to change the size of icons in QMenu. I don't see any method like "setIconSize()" for QMenu. Simple code:

action = new QAction(icon, "Settings", this);
menu->addAction(action);

All icons that I create like this appear in the menu in small size (a bit smaller than in all other programs). Is it possible somehow to increase the size of icons? I use *.svg and *.png icons of different sizes from 48x48 to 128x128, but when my program runs, all icons have the same default and small size.

Thank you!

patrik08
2nd September 2008, 23:53
Hi, I can't find how to change the size of icons in QMenu. I don't see any method like "setIconSize()" for QMenu. Simple code:

action = new QAction(icon, "Settings", this);
menu->addAction(action);

All icons that I create like this appear in the menu in small size (a bit smaller than in all other programs). Is it possible somehow to increase the size of icons? I use *.svg and *.png icons of different sizes from 48x48 to 128x128, but when my program runs, all icons have the same default and small size.

Thank you!

On ToolBar i write:
toolBar->setIconSize(QSize(_MAINICONSIZE_,_MAINICONSIZE_));

on the place that you play action i am sure is the same method.....

jpn
3rd September 2008, 06:02
Please search the forums.

QMenu uses styles to decide the correct icon size for the platform in use. You can trick QMenu to use larger icon size with a custom style. For more details, see thread: http://www.qtcentre.org/forum/f-qt-programming-2/t-why-does-qmenubar-resize-icons-4173.html.

alex chpenst
3rd September 2008, 12:16
Thanks a lot, it works fine. With the help of this link and some code from there I came to the following (a bit repeating but more detailed).

1. Find out which style you use:

QApplication app(argc, argv);
std::cout << "Default Style: " << app.style()->metaObject()->className() << std::endl;

In my case QCleanlooksStyle is used by default.

2. make mystyle.h

#include <QCleanlooksStyle>
class MyStyle: public QCleanlooksStyle
{
Q_OBJECT
public:
int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0 ) const {
int s = QCleanlooksStyle::pixelMetric(metric, option, widget);
if (metric == QStyle::PM_SmallIconSize) {
s = 20;
}
return s;
}
};

3. Apply your style to your application

#include "mystyle.h"
...
QApplication app(argc, argv);
app.setStyle(new MyStyle);

That's it. If another style is used, e.g. QPlastiqueStyle, replace QCleanlooksStyle with QPlastiqueStyle and it should work as well.

jpn
3rd September 2008, 14:16
You could have also used a proxy style as proposed in the another thread. :)