PDA

View Full Version : RollOver on a QtPushButton (or QtToolButton)



Snick
11th November 2009, 22:12
I'm trying to make a normal button on Over effect (on qt 4.5) : when you go over a button it switch to a different image, when you leave it it turn back to the normal image.

i write this lines:

QToolButton* dice= new QToolButton(this);
QIcon icon;
icon.addPixmap(QPixmap("img/gioca_on.png"),QIcon::Active);
icon.addPixmap(QPixmap("img/gioca_off.png"),QIcon::Normal);
dice->setIcon(icona);
dice->setIconSize(QSize(103, 43));
dice->setGeometry(50, 50, 103, 43);




probably i don't get at all how to use the MODE parameter on the addPixmap method.
any suggestion.



On the same topic, what about if i want to use alpha-png (so with transparence) and don't want to show the normal PushButton Gui under the transparence?

tnx all

miwarre
12th November 2009, 10:53
QToolButton* dice= new QToolButton(this);
QIcon icon;
icon.addPixmap(QPixmap("img/gioca_on.png"),QIcon::Active);
icon.addPixmap(QPixmap("img/gioca_off.png"),QIcon::Normal);
dice->setIcon(icona);
dice->setIconSize(QSize(103, 43));
dice->setGeometry(50, 50, 103, 43);


Well, assuming "icona" in line 5 is a typo for "icon" (the compiler would catch it anyway), I had some success, using QIcon::addFile() rather than QIcon::addPixmap(). It needs an additional size parameter. The file parameter can be a regular file or a resource. Something like this:


QPushButton * pButton;
QIcon icon;
QSize size(MY_BUTTONW, MY_BUTTONH);

pButton = new QPushButton("", parent);
pButton->resize(size);
pButton->setIconSize(size);
icon.addFile(strIconNormalFName, size, QIcon::Normal);
icon.addFile(strIconActiveFName, size, QIcon::Active);
pButton->setIcon(icon);
pButton->show();
I assume it should also apply to QToolButton but I didn't check. However, I run into a problem anyway: if the button has the focus, it is shown with the active image regardless of the mouse position.

On the same topic, what about if i want to use alpha-png (so with transparence) and don't want to show the normal PushButton Gui under the transparence?
This seems contradictory: if you do not want the graphics underneath to show through, do not use transparence. Or am I missing something?

Ciao,
M.

Snick
12th November 2009, 17:25
Tnx you for the quick answer,

yes icona was a typo for icon.

regarding the second question, the main problem is: i just want a pushable image that change when you are over it, so at the end seams the quickly solution using the Qtoolbutton or the QPushbutton to doing this.

sometimes you need to use alpha image on doing this, and if there any way to hide the "normal" background, i can keep using the Qtoolbutton , instead of creating a new class for doing this ;)

miwarre
13th November 2009, 23:16
I suspect you only have to try (as I still do not understand the need for alpha in this case).

However, if you have several buttons of that kind, subclassing the Qt class may be easier and lead to cleaner code than repeating a possibly complex customization for each button.

Wishing you good luck,

M.

Archimedes
14th November 2009, 13:35
You can reimplement the mouseMoveEvent function and check when the button is hovered. If it's hovered update and in paintEvent function change the icons for the button.