PDA

View Full Version : QToolButton and Style sheet



desch
28th November 2007, 10:26
Hi everybody,

Description

I have a QAction with an icon
the QAction is set in Qmenu with

menu->addAction(myAction)

on a widget inside the application, I have a QToolButton with the following code

myQToolButton->setDefaultAction(myAction)
this is done to have different access to the same action in different place

the style sheet is the following:



QToolButton#myQToolButton{image: url(:/image.png);}
QToolButton#myQToolButton:hover{image: url(:/image_rollover);}
QToolButton#myQToolButton:pressed{image: url(:/image_pushed);}
QToolButton#myQToolButton:disabled{image: url(:/image_disabled);}



so the first question:
In understand that the QtoolButtoin is connected with the action but
how to remove the icon in QtoolButton to set correctly the style sheet inside?

So my second question relative to the first question:
If it's not possible to remove icon, How to set the style to an action inside a QMenu in order to get the same style to the Qmenu AND to the ToolButton


Thanks to all

David

wysota
28th November 2007, 18:31
Do you have to use the stylesheet? Can't you use what QIcon offers?

desch
28th November 2007, 21:39
That's not so easy .... The whole application is based on Style sheet; some buttons change their pictures when they are toggled... others get differents states with different icones

it's more beautiful ... yes I know :rolleyes: not very useful... :D


Anyway if Qicon can make all states in rollover, pressed , disabled, and so an tell me, I will code for that ; but could you tell me how to do that...

Thanks

pozdrowienia !

David

wysota
28th November 2007, 23:03
Take a look at QIcon::addPixmap and its parameters. You can add different pixmaps for different icon states.

desch
30th November 2007, 11:57
I test it in different case
but it seems that's not working for the "pushed mode" on not checkable buttons;

this is a property we use constantly, and it sems We should subclass mouseMoveEvent for a QToolButton and install and Event Filter

Somethings I don't want for just setting icones.

If I go on with style sheet, is there a way to subclass paintEvent in order to remove the iCon inside the QtoolButton and let go the style sheet through the QToolButton ,
it could be done, no ??? :cool:

Thanks for your help

David

wysota
30th November 2007, 12:17
If you override paintEvent, you don't have to use stylesheets but instead do your completely custom painting based on your own set of icons.

desch
30th November 2007, 13:16
Ok , could you just post a little piece of code about that

Thanks

wysota
30th November 2007, 14:24
void X::paintEvent(QPaintEvent *pe){
QPixmap px;
if(...){
px = QPixmap("p1.png");
} else if(...){
px = QPixmap("p2.png");
} else ...
QPainter p(this);
p.drawPixmap(rect(), px);
}

desch
1st December 2007, 09:38
I find exactly What I have to do;

state are : Normal, Rollover, Pushed/on, Disabled
state not implemeneted are pushed mode.

In order to get an icone for different state, independant from the style

I do that:


QIcon icon;
icon.addFile(":/Normal.png", QSize(), QIcon::Normal, QIcon::Off); // Normal
icon.addFile(":/Rollover.png", QSize(), QIcon::Active, QIcon::Off); //rollover
icon.addFile(":/Pushed.png", QSize(), QIcon::Active, QIcon::On); //pushed/On
//disabled is already implemented

_myAction->setIcon(icon);
myToolButton->setDefaultAction(_undoAct);



In a subclass of QtoolButton, I override the paintEvent like that:



void CAdvToolButton::paintEvent(QPaintEvent * e)
{
// case for pushed mode
if (isEnabled() && isDown())
{
QStylePainter p(this);
QStyleOptionToolButton opt;
initStyleOption(&opt);

// modify style Option: "icon", to get a new icon inside
opt.icon = QIcon(icon().pixmap(iconSize(), QIcon::Active, QIcon::On));

p.drawComplexControl(QStyle::CC_ToolButton, opt);
}
else
QToolButton::paintEvent(e); // all other mode are supported

}


that'all :)

David