2

I would specifically like to get the active on/off name of the icon for the qPushButton. I tried to log first the icon name like on my code but the output was always empty even if I tried to programmatically make an icon, any idea how to solve this?

Here's my code for just trying to log the icon name:

qDebug() << "icon name " << ui->btnWestern1_2->icon().name(); //output: empty string

Just to verify my objective is to get the active on/off name not just the icon name but nothing works for me.

I'm using Qt 5.2.1

enter image description here

Garlen
  • 426
  • 1
  • 5
  • 17

1 Answers1

3

You can't. the icon name method is not useful in your case.

The icon is created with a filename but the filename is not stored anywhere. See this answer

You can check the way your button is created by looking at the uic ui_XXX.h file.

If you want to do it progmatically you can a property to the button containing the two icons (on/off)?

Here is a (not tested) hint:

QStringList icons = (QStringList() << "icon1.png" << "icon2.png");

QIcon my_icon;
my_icon.addFile(QStringLiteral(icons[0]), QSize(), QIcon::Normal, QIcon::Off);
my_icon.addFile(QStringLiteral(icons[1]), QSize(), QIcon::Normal, QIcon::On);

QPushButton btn = new QPushButton(my_icon);
btn->setProperty("icons",icons);

qDebug() << btn->property("icons");
Community
  • 1
  • 1
bibi
  • 3,671
  • 5
  • 34
  • 50
  • Thanks for the answer, but may I know when does "name" method can be useful or be functional? – Garlen Mar 22 '16 at 14:26
  • looks like it is used in case of using a theme : http://doc.qt.io/qt-4.8/qicon.html#name – bibi Mar 22 '16 at 14:37