PDA

View Full Version : Where are the QIcons in QPushButton



tescrin
21st July 2012, 00:09
In Qt Designer there are 8 icons (properties) listed in the QAbstractButton class:
Normal (on/off)
Active (on/off)
Toggled (on/off)
Selected (on/off)

I don't actually know (nor can I find) whether these are just *POSSIBLE* storage locations or whether or not somewhere in QAbstractButton it stores these eight QIcons. If someone could advise me as to whether I can use these icons in a derived class or whether I need to provide my own storage that'd be lovely! As it is I've checked source code and documentation up and down and there's nothing pointing me in the direction of these supposed properties. There's not even Q_PROPERTYs declaring what happens for these.

If these are real QIcons, where are they stored/how do I access them. I don't want to clutter my implementation with needless additional storage if I don't have to. Thanks!

ChrisW67
21st July 2012, 00:26
There is only a single QIcon set on a QAbstractButton using setIcon(). That QIcon can have a pixmap associated with each QIcon::Mode and QIcon::State using QIcon::addPixmap() or QIcon::addFile(). If you do not set a pixmap for a given mode/state then one is derived from the base (normal/off) icon. See the QIcon details.

tescrin
21st July 2012, 00:42
Many thanks for the quick reply. I'm looking at the example and having a hard time how I should be setting the currently displayed icon based on these states and such. Do I really need to take a QPixmap and use the painter directly every time?

I.E. how do I use QIcon's Mode and State enums to set the current icon based on them. Say I have an Icon:
Do I set it as Active when I want? If so will it automatically update the icon? If so how do I do this?
Do I instead derive the pixmap from the state/mode? If so, how do I use this pixmap?

wysota
21st July 2012, 02:47
Why don't you read the docs on QIcon? There is a nice example there showing the concept of different icon modes.

ChrisW67
21st July 2012, 04:46
I.E. how do I use QIcon's Mode and State enums to set the current icon based on them.
You don't. There is no notion in QIcon of a current state, just a collection of QPixmaps that can be used by classes that render QIcons.

Say I have an Icon:
Do I set it as Active when I want? If so will it automatically update the icon? If so how do I do this?
Do I instead derive the pixmap from the state/mode? If so, how do I use this pixmap?
If the specific QAbstractButton subclass you are using has a state that has a meaningful mapping to one of the QIcon modes then the button can use the pixmap for that mode when the button paints itself. If you are implementing you own QAbstractButton sub-class and you want it to use the non-default QIcon pixmaps then, yes you do need to account for that in your QWidget::paintEvent().

For the vast majority of purposes QPushButton or QToolButton are the classes you will be using for button-like behaviour... and they already take care of using a suitable pixmap from the QIcon to match the button's state.

tescrin
23rd July 2012, 17:15
I think I need to redefine the problem more concretely. I'm attempting to design a specific button to be plugged into designer. That's all fine and good. The problem is:

-I need to know how to manipulate the current mode/state of a qpushbutton. I don't see the enums listed under QAbstractButton nor do I see them under QPushButton. There seem to be no functions for it either. I'm guessing they're there, but I can't find them. This is why I mentioned that I also found no Q_PROPERTYs to clue me into where I should be looking.

Instead I can add my own modes/states to the button I made, but I feel like there are enough states in QPushButton that this is extraneous. I have to manipulate the image of the button in a specific way, so being able to manipulate/check the current state is important.