How to set an application icon in Qt

The Beast picture The Beast · Jan 21, 2016 · Viewed 9.4k times · Source

I have some trouble trying to set an icon for my QT application.

The icon is named "room.ico" and is on the same directory as the source file.

Here is the code :

#include <QApplication>
#include <QWidget>

int main( int argc, char *argv[ ] )
{
   QApplication app( argc, argv) ;
   QWidget fenetre;
   fenetre.setWindowIcon(QIcon("room.ico")); // Nothing happens
   fenetre.setWindowTitle("Heloo");    
   fenetre.show();
   return app.exec() ;
}

I have tried to add win32:RC_ICONS += room.ico in the .pro file but that didn't work. I have also tried "./room.ico" but still no icon.

I have tried to use this :

QPixmap pixmap = QPixmap ("room.ico");
fenetre.setWindowIcon(QIcon(pixmap));

And guess what !!! it didn't work ... i'm just a newbie to QT :p

Any suggestions will be appreciated , thanks

Answer

Tas picture Tas · Jan 21, 2016

QT's documentation for QWindow::setWindowIcon should be what you need.

  1. Make an icon file (you appear to have done this already: room.ico
  2. Add your icon file to a QT resource file (.qrc or .rc) which you should add to your project (the documentation discusses how to do this
  3. Use setWindowIcon and pass in a QIcon:
    1. app.setWindowIcon(QIcon(":/room.ico")); (this assumes your file is in the resource file)

Your problem appears to be that you didn't prepend :/ when passing in the filename to QIcon.