PDA

View Full Version : How do I Use Icons in My Program?



Chase
24th April 2011, 11:41
I'm trying to figure out how to show an icon on a QPushButton. Hope you can help me out.

I set the icon (member of a resource file) using Qt Designer, and it shows up fine in Preview mode but not in my compiled program (the icon won't show up at all). Following are the relevant lines from the .ui file:



public:
QPushButton *btnPrs; // Parse button
...

void setupUi(QWidget *frmWindow)
{
...
btnPrs = new QPushButton(frmWindow);
btnPrs->setObjectName(QString::fromUtf8("btnPrs"));
QIcon icon;
icon.addFile(QString::fromUtf8(":/Go.png"), QSize(), QIcon::Normal, QIcon::Off);
btnPrs->setIcon(icon);
btnPrs->setFlat(false);
...
Some more facts:
I've added
Q_INIT_RESOURCE(resrc); before QApplication instantiation in main.cpp.

I've also added
RESOURCES += ./Resources/resrc.qrc to the project file.

The project's directory tree is as follows:

/ParserTest/ (containing all the .h, .ui, .cpp files and a .pro file)
- ./Resources/ (containing all .ico and .png files and the resrc.qrc file)
- ./Etc/ (Not used yet)

I'm not using prefixes in the .qrc.

This is on 32-bit Windows, Qt 4.7.2 (pre-compiled binaries) + MinGW 4.4.0 + Qt Creator 2.1.0.

This problem also occurs with *.ico files.

What else should I do?

Added after 1 5 minutes:

If I move the icon loading affairs from the form constructor -- formClass::setupUi() -- to my application window constructor, the icon shows up. But what could be the problem? Is it possible to keep icon loading codes in setupUi() to keep my codes well-organized?

wysota
25th April 2011, 11:11
Please provide a minimal compilable example reproducing the problem.

Chase
25th April 2011, 11:32
Sorry, I no longer keep the old files. My bad...

I decided to create a new project and pasted all codes from the old one (except UI and project files). I then rebuilt the entire UI's in Qt Creator and made some adjustments to the project file. Everything's been working fine now. All icon-loading codes are now back to where they belong -- setupUi().

Thanks for trying to help.