0
votes

I'm working on a project that has 40 checkable push buttons in a group and I want the icon of one button to change depending on a value I define. I'm not creating a new pushbutton, just changing the icon of the current one. So I'm testing it by trying to change one button's icon.

Here is my current code:

QPixmap b1d0(":/textures/blocks/textures/blocks/stone.png");

QIcon ButtonIcon(b1d0);
ui->slot_0->setIcon(ButtonIcon);
ui->slot_0->setIconSize(b1d0.rect().size());

The resource path was copied directly from my resource file so it is correct, I've messed with it like crazy but no change so...

slot_0 is my pushButton.

What did I do wrong? Or better yet, am I even allowed to change the icon of an existing pushButton?

Thanks for your time :)

1
Check if b1d0.isNull() returns true. If it do, then it is not finding the image file or failing to read it. - Guilherme Bernal
Interesting. It is true, yet I copied the path directly from my resource file? I'll keep messing with it. - mrg95
So I changed the path to a picture file directly on my C: drive. It stayed null. Do you think I can't put a png into a pixmap or something? - mrg95
Try with some other image file. Also check the return of QFile::exists("..."). - Guilherme Bernal
Which compiler are you using? On MSVC, you'll need to add Q_INIT_RESOURCE(res); to the beginning of main() where res is the name of your .qrc file (without the .qrc). With GCC, it just works. - Alex Reinking

1 Answers

1
votes

If you are on Windows using MSVC as your compiler, and want to use Qt's resource system, you will need to add

Q_INIT_RESOURCE(res)

to the beginning of main(), where res is the name of your .qrc file without the ".qrc".

To get a full view of this, look at this gist:

https://gist.github.com/alexreinking/5992821