PDA

View Full Version : Why does QMenuBar resize icons?



drhex
25th October 2006, 20:28
Hi all,

In the small example below I am inserting a small pixmap into a QAction in a
QMainWindow's menubar. I'm using a pixmap ("key.png") that is 19 by 9 pixels,
but QT resizes it to 16x7.

A 19x9 pixels image would easily fit on the menubar, so why does QT feel a need to resize? Is there a way to have it shown in its original size?

(QT4.2, X-Windows)

/Joakim



#include <QApplication>
#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>


int main( int argc, char **argv )
{
QApplication app( argc, argv );

QMainWindow *mw = new QMainWindow();
mw->show();

QMenuBar *mb = mw->menuBar();
QMenu *menu = new QMenu("File");
mb->addMenu(menu);
menu->addAction("About", &app, SLOT(aboutQt()));

QAction *act = mb->addAction("");
act->setIcon(QPixmap("key.png"));

app.exec();
return 0;
}

jpn
25th October 2006, 20:48
QMenu uses QStyle::PM_SmallIconSize. You could use a custom style and provide larger PM_SmallIconSize.

drhex
26th October 2006, 20:32
Alright, I found out what style the menubar was using by calling metaObject()->className() on it. It was QPlastiqueStyle,,
so I replaced the style with an instance of the class below:



class myStyle: public QPlastiqueStyle
{
Q_OBJECT
public:
int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0 ) const {
int s = QPlastiqueStyle::pixelMetric(metric, option, widget);
if (metric == QStyle::PM_SmallIconSize) {
s = 20;
}
return s;
}
};


Small icons can now be upto 20x20 pixels instead of 16x16. And it works!
But one thing still bothers me, I had to hardcode "QPlastiqueStyle". What if another style had been specified on the commandline? Is there a way to modify the behaviour of the current style rather than forcing in a specific style?

jpn
26th October 2006, 20:40
But one thing still bothers me, I had to hardcode "QPlastiqueStyle". What if another style had been specified on the commandline? Is there a way to modify the behaviour of the current style rather than forcing in a specific style?
There's a technique called proxy style (http://wiki.qtcentre.org/index.php?title=QStyle). A proxy style redirects all other than overridden calls to the "original" style.

duduqq
7th July 2008, 14:20
can you give me you code ,I have the same proble,I am a new guy at qt

could you sent a email to me
ouyangzy2005@163.com

Farseer
19th November 2010, 13:28
Hi, i use above code
int s = QPlastiqueStyle:: pixelMetric(metric, option, widget);
if (metric == QStyle::PM_SmallIconSize) {
s = 50;
}
then the icon and text overlap, it seems the text position doesn't change when the icon become large.
How to solve this problem?