PDA

View Full Version : Change button Icon on toggle



bizmopeen
13th July 2009, 18:31
Hello, all;

I'm trying to program a QPushButton or QToolButton to change the icon being displayed when toggled/activated. Unfortunately, I'm on a shard system and unable to use QSetIcon or add it to the library. Any suggestions on how to proceed?

Thanks in advance,

Allan

calhal
13th July 2009, 19:38
Probably you want to use QIcon class . You can find example how to use it with QPushButton in Assistant.

If you want to toggle the icon, you should connect signal clicked() or toggled() of your button to your custom slot and in that slot just call QPushButton::setIcon() method.

Again, read about QPushButton and QIcon in Assistant.

If your button is checkable you can also do it without signal and slot. Here's an example:

QToolButton *b = new QToolButton(this);
QIcon *ico = new QIcon();
ico->addPixmap(QPixmap("on.jpg"),QIcon::Normal,QIcon::On);
ico->addPixmap(QPixmap("off.jpg"),QIcon::Normal,QIcon::Off);
b->setIcon(*ico);
b->setCheckable(true);



Unfortunately, I'm on a shard system and unable to use QSetIcon or add it to the library
I really don't understand what do you mean. Could you elaborate?

bizmopeen
13th July 2009, 23:07
I really don't understand what do you mean. Could you elaborate?

Sorry, that was supposed to read "I'm on a shared system". (Fat Fingers strikes again...) Basically, the documentation for QSetIcon mentions the need for the "qseticon.h" header file, which I can't add to the library because I'm not the Administrator and don't have access. In any event, I will try the method you posted above, many thanks!

bizmopeen
13th July 2009, 23:17
Success! Thank you very much, calhal.