0

I tried to set an image to a Qt pushbutton using this widely known code.

QPixmap *pic = new QPixmap(":/images/logo.png");
QIcon *icon = new QIcon(*pic);
ui->pushButton->setIcon(*icon);
ui->pushButton->setIconSize(QSize(pic->width(), pic->height()));

Here's my qrc file

<RCC>
    <qresource prefix="/images">
        <file>images/logo.png</file>
    </qresource>
</RCC>

Even though the program compiles, always a runtime exception occurs at

QPixmap *pic = new QPixmap(":/images/logo.png");

When I tried printing *pic on output console, it showed that pic = QPixmap(QSize(0, 0) ). i.e. it is null. Any ideas as to where I went wrong?

Thanks in advance!

Lizzy
  • 2,033
  • 3
  • 20
  • 33

4 Answers4

3

Please don't use pointers there.

You don't even have to create a QPixmap object and then use that to create a QIcon object. QIcon has a constructor that takes a file name as a parameter. But that's up to you. Also check your resource location. It looks like it should be :/images/images/logo.png

QPixmap pix(":/images/images/logo.png");
QIcon icon(pix);
ui->pushButton->setIcon(icon);
ui->pushButton->setIconSize(pix.size())
thuga
  • 12,601
  • 42
  • 52
  • ah yes! didn't think of that constructor. Anyway, I modified my code. Now QPixmap object gets initialized fine. But still the program crashes at QIcon icon(pic); why would that be? – Lizzy Nov 14 '13 at 09:47
  • @Lizzie If you create an empty pixmap does it still crash? `QPixmap pic;` `QIcon icon(pic);` – thuga Nov 14 '13 at 10:24
  • yes it still does. It is similar to my earlier problem where pic is actually empty, isn't it? – Lizzy Nov 14 '13 at 10:30
  • @Lizzie Yes, but it shouldn't cause a crash. Are you sure that it's `QIcon icon(pic);` that causes the crash? – thuga Nov 14 '13 at 10:51
  • sorry! my bad. It's actually at this line. ui->pushButton->setIcon(icon); – Lizzy Nov 14 '13 at 11:11
  • @Lizzie Are you doing this before the `ui->setupUi(this);` line? Cause if you are, then `ui->pushButton` is not an initialized object. – thuga Nov 14 '13 at 11:24
  • Yes! that was the problem! thanks @thuga. I'll mark this as the correct answer :) – Lizzy Nov 14 '13 at 11:34
3

If you use an alias in your resource definition, using the resource is a little easier: -

<RCC>
    <qresource prefix="/images">
        <file>images/logo.png</file>
        <file alias="logo">images/logo.png</file>
    </qresource>
</RCC>

Then you can reference the resource via its alias and if you change the image in the resource, you won't need to change the code: -

QPixmap image(":/images/logo");
QPushButton* pPushButton = new QPushButton(QIcon(image));

Or if the button is already defined: -

pPushButton->setIcon(QIcon(image));
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
1

Is your Resource properly initialized? You might try this: (from the docs)

If you have resources in a static library, you might need to force initialization of your resources by calling Q_INIT_RESOURCE() with the base name of the .qrc file. For example:

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     Q_INIT_RESOURCE(graphlib);
     ...
     return app.exec();
 }

EDIT More important:

QPixmap assumes a 'images' prefix. Try QPixmap(":/logo.png");

Thalia
  • 13,637
  • 22
  • 96
  • 190
Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46
1

I think you should call:

QPixmap *pic = new QPixmap(":/images/images/logo.png");

because you already set the prefix /images.

vahancho
  • 20,808
  • 3
  • 47
  • 55