PDA

View Full Version : How to change the icon size in QMenu?



harinlen
14th June 2013, 07:12
Hi, I am a beginner of Qt, and I am also a foreigner. Please ignore all the strange spelling and grammar error~Thanks a lots~

Now I got some issue with the QMenu. I've already know the QStyle::PM_SmallIconSize in my style. I have set that value to 50.
But the problem is: the blank has been increase. The image didn't change. Still 16x16.

I have try to find out some other way to change the size of image. But nothing seems to work.

What can I do about this? Thanks for everyone's help!

karankumar1609
14th June 2013, 09:15
I think this is what you want.
A simple search on google gives me the link (http://www.qtcentre.org/threads/15742-How-to-change-Icon-size-in-QMenu).

http://www.qtcentre.org/threads/15742-How-to-change-Icon-size-in-QMenu

harinlen
14th June 2013, 12:07
Thanks for your quick reply.

I've already read this. But it seems works nothing.
Actually I changed PM_SmallIconSize value to 50. But only the height of QMenuItem increase. The image didn't changed.
9155
This is what the SmallIconSize changed works.

karankumar1609
14th June 2013, 12:29
Search is the best friend of yours.
Have some good search and got:
http://www.qtcentre.org/threads/4173-Why-does-QMenuBar-resize-icons


Try this one ... it will resolve your problem.


CHEERS...!!!

harinlen
14th June 2013, 13:41
Thanks Again, but there might be some problems with my own style...
I've find out where the problem might be.

This article I have read, too. These two article is actually the same one. Because the solution is totally the same.
Now I'm busy coding with my own style, thanks all the same~

wysota
15th June 2013, 10:14
What is the size of the images you are setting as icons?

harinlen
16th June 2013, 08:57
The icon is 30px30p. But when I add the icon to QMenu, it looks just the same like the snapshot on #3.

Added after 4 minutes:

Actually, I find out that I have changed Style::drawMenuItem in my own style, and I rewrite it. I draw the new icon and now it really works.
But a new problem: I use switch(opt->menuItemType) to judge the type and redraw the menu, I find out that the MenuItem which I put the mouse over it doesn't changed. I searched for the opt->state, but nothing seems to work. Could you please help me with this problem? Thanks a million~

wysota
17th June 2013, 10:21
The icon is 30px30p. But when I add the icon to QMenu, it looks just the same like the snapshot on #3.
What if you set a larger icon? As far as I remember Qt does not upscale icons.

harinlen
18th June 2013, 03:40
I have tried several times, nothing happened. The Size of Icon is just look like that snapshot.
I'm so sorry that I forget to tell you about the style which I rewrite:


void KreogistCuteStyle::drawControl(ControlElement element,
const QStyleOption *opt,
QPainter *p,
const QWidget *w) const
{
switch(element)
{
case CE_MenuItem:
{
const QStyleOptionMenuItem *_opt_menu_item=
qstyleoption_cast<const QStyleOptionMenuItem *>(opt);
if(_opt_menu_item != nullptr)
{
drawMenuItem(_opt_menu_item,p,w);
}
break;
}

default:
style->drawControl(element,opt,p,w);
break;
}
}

void KreogistCuteStyle::drawMenuItem(const QStyleOptionMenuItem *opt,
QPainter *p,
const QWidget *w) const
{
switch(opt->menuItemType)
{
case QStyleOptionMenuItem::Normal:
{
int iRectX=opt->rect.x(),
iRectY=opt->rect.y(),
iRectW=opt->rect.width(),
iRectH=opt->rect.height();
QPen pen;

if(opt->state == State_Selected)
//opt->state == State_HasFocus)
{
p->setBrush(opt->palette.highlight());
p->drawRect(opt->rect);
pen.setColor(QColor(0xf6,0xcd,0x32));
p->setPen(pen);
p->fillRect(opt->rect,Qt::BrushStyle::SolidPattern);
qDebug()<<"asdf";
}
else
{
;
}
if(!opt->icon.isNull())
{
p->drawPixmap(13,
iRectY + (iRectH - 30)/2,
30,
30,
opt->icon.pixmap(30, 30));
p->drawText(50,
iRectY,
iRectW,
iRectH,
Qt::AlignVCenter,
opt->text);
}
else
{
p->drawText(50,
iRectY,
iRectW,
iRectH,
Qt::AlignVCenter,
opt->text);
}
pen.setColor(QColor(0,0,0));
p->drawLine(iRectX,iRectY+iRectH-1,iRectX+iRectW,iRectY+iRectH-1);
break;
}
default:
style->drawControl(CE_MenuItem,opt,p,w);
}
}

I rewrite these two things to resize the icon and draw lines, but I find out the selected item doesn't change into highlight. How can I solve the problem?

wysota
18th June 2013, 05:42
If you are drawing the icon yourself and fixing its size at 30px then I don't know why you would expect the size of the icon to be different.

harinlen
19th June 2013, 05:21
Well, I solved this problem by using this style. Thanks a lot !