PDA

View Full Version : Using ICONs as PushButtons



sudhakaran
3rd February 2010, 10:16
I am developing a simple GUI application using Qt 4.5 (on a windows PC).

I wanted to make a cute push button using my own icons.

I tried using the QPushButton class and a simple image as a pixmap. I could always see a rectangular outline around the icon, whenever it was clicked.
How to do away with that.

I also created a QIcon object with two icons (*.png files) one for the state: "Normal" and the other for the state: "Selected". (Refer the attachments).
But couldnt get the clue on how to link the push button states to these icons.

So, now my questions are:
1. How to use image files like *.png, to create a custom push button (without getting the rectangular outline)
2. Is it always a must to use QPushButton class for this, OR do we have an alternative for realizing the above?

- Sudhakaran

high_flyer
3rd February 2010, 14:06
1. How to use image files like *.png, to create a custom push button (without getting the rectangular outline)
see setMask().


2. Is it always a must to use QPushButton class for this, OR do we have an alternative for realizing the above?
nothing is a must.
The questions is, what functionality so you need, which QPushButton does not offer you?

psih128
3rd February 2010, 16:28
QAbstractButton has the icon property, which holds a QIcon variable. Take a look at QIcon documentation - This class allows you to specify separate images for different icon states.

boudie
3rd February 2010, 23:35
Try using a QToolButton instead of a QPushButton.

Lykurg
4th February 2010, 08:12
You also can use a simple QWidget as a base class and then reimplement mouse press events and emit a clicked signal by your own...

BenPa
4th February 2010, 10:00
Take a look at QIcon documentation - This class allows you to specify separate images for different icon states.
How can I specify different images for different modes in QIcon? I've seen that there is an enum "Mode" to identify the state, but I don't see how to add different images to the Icon representing the modes ...

high_flyer
4th February 2010, 11:22
You can connect a slot to the button, check the state, and set the correct icon for the given state.

BenPa
4th February 2010, 12:44
You can connect a slot to the button, check the state, and set the correct icon for the given state.
But for what do I need the "Mode" enum defined in the QIcon class? I don't see a function to set a mode for a QIcon. Using different modes in a icon makes sense to show different states, but if I have to implement this functionality myself, I don't understand for what the mode is defined in the class..

Thank you for the tip :).

high_flyer
4th February 2010, 13:31
Its all in the QIcon documentation.
Qt can generate disabled/selected icons automatically using widely used conventions.
If these conventions suit you, you can certainly use these auto generated icons.
QPushButton will show the icons correctly depending on the state.

But your case is different, since you want to use different images for various states, so you have to implement it your self.

Coises
4th February 2010, 18:24
How can I specify different images for different modes in QIcon?
Use QIcon::addFile or QIcon::addPixmap.

If your use fits within the paradigm of four modes (normal, disabled, active and selected) and two states (on and off), you can use this method to specify up to eight images for one QIcon. If that matrix does not reasonably represent the set of images you need, then you’ll need to develop your own method of switching images; you could do that in various ways, such as by re-implementing QWidget::paintEvent or by calling QAbstractButton::setIcon (http://doc.trolltech.com/latest/qabstractbutton.html#icon-prop) whenever the correct image to display changes.

Coises
4th February 2010, 18:34
I don't see a function to set a mode for a QIcon.

You don’t set the mode of a QIcon; you choose the desired mode and state when you paint the icon with QIcon::paint or retrieve an image with QIcon::pixmap. When using a class derived from QAbstractButton, the button takes care of painting the appropriate image depending on the mode and state of the button, so you can add all the images to the icon and then be concerned only with the mode and state of the button, without thinking any further about the icon.