Set icon on qpushbutton

user2372976 picture user2372976 · Nov 4, 2013 · Viewed 9k times · Source

I know, thats a beginners question but I have to tried to solve it with goolge / stackoverflow, but I haven't found a good answer. The problem is, I want to add a icon to a qpushbutton. But it doesnt work / the file is not found?!, where is the problem?

...pro-File

[...]OTHER_FILES += \
Readme.txt \
icons/newFolder.png \
icons/newFile.png 

class.cpp

//toolbar
QToolBar *tool = new QToolBar();
QPushButton *btn = new QPushButton();
btn->setIcon(QIcon(":/icons/newFile.png"));

tool->addWidget(btn);
addToolBar(tool);

Best regards :D Chris

Answer

Pavel Strakhov picture Pavel Strakhov · Nov 4, 2013

Adding PNG files to your project doesn't have any effect. You need to create a resource file and add your files to it. After doing this you can access resource data using ":/icons/newFile.png" notation.

See The Qt Resource System.