0

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 :)

mrg95
  • 2,371
  • 11
  • 46
  • 89
  • Check if `b1d0.isNull()` returns `true`. If it do, then it is not finding the image file or failing to read it. – Guilherme Bernal Jul 14 '13 at 00:54
  • Interesting. It is true, yet I copied the path directly from my resource file? I'll keep messing with it. – mrg95 Jul 14 '13 at 00:56
  • 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 Jul 14 '13 at 01:01
  • Try with some other image file. Also check the return of `QFile::exists("...")`. – Guilherme Bernal Jul 14 '13 at 01:05
  • Okay, so I moved the stone.png to the project directory so I don't need and "\\" or "/". But then I wonder, what is wrong with my resource path? – mrg95 Jul 14 '13 at 01:12
  • 1
    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 Jul 14 '13 at 01:13
  • I added that and changed the name, yet I'm getting a link error? – mrg95 Jul 14 '13 at 01:16
  • TO be more precise: main.obj:-1: error: LNK2019: unresolved external symbol "int __cdecl qInitResources_icons(void)" (?qInitResources_icons@@YAHXZ) referenced in function _main – mrg95 Jul 14 '13 at 01:19
  • Well, maybe this will help. It's a complete example of a pushbutton that toggles between two icons as it's pressed: https://gist.github.com/alexreinking/5992821 – Alex Reinking Jul 14 '13 at 01:23
  • That is exactly what I did. Ugh http://gyazo.com/7495233a747fad30e58ef4047ffda11b – mrg95 Jul 14 '13 at 01:25
  • Are you using qmake to build everything? If not, how are you linking the generated files from the .qrc to your project? It'd be like qrc_*.cpp. – Alex Reinking Jul 14 '13 at 01:31
  • oops, I just had to clean all, run qmake, then rebuild. Thanks so much for you help :) – mrg95 Jul 14 '13 at 01:32

1 Answers1

1

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

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86