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!
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())