0
votes

I have just started using Qt and I've been trying to get a single button to display an icon but for some reason I can't get it to work. This is my code

QPushButton* button = new QPushButton();
button->setIcon(QIcon("icon.png"));
button->show();

I don't know what I'm missing here, the icon.png and the .cpp file are both in the same folder.. What am I doing wrong?

1
the icon.png must be where the executable file is, not where the cpp is, or much better you could put your icon in a resource file. - Marco
Welcome to StackOverflow! Provide a Minimal, Complete, and Verifiable example to make sure your question gets proper attention. - Volodymyr Lashko
There are 3 simple solutions, the first place the complete path of the image, or use a qresource to store the image, or place the image in the same folder of your .app. - eyllanesc
The icon.png is currently in the same folder as .app, still can't get it to show - Josh3248
Try placing the complete path of your image. - eyllanesc

1 Answers

3
votes

Add icon folder to your resources.qrc and try this one:

setIcon(QIcon(QPixmap(":/icons/yourIcon")));

It works in my project