4

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

Alexander V
  • 8,351
  • 4
  • 38
  • 47
The Beast
  • 1,629
  • 2
  • 29
  • 42

1 Answers1

6

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.

Peter Kühne
  • 3,224
  • 1
  • 20
  • 24
Tas
  • 7,023
  • 3
  • 36
  • 51
  • Make sure you suggest the proper string in function argument. – Alexander V Jan 21 '16 at 01:41
  • @AlexanderVX Ah well spotted, thanks. I've updated the answer. – Tas Jan 21 '16 at 01:45
  • 2
    @Tas What i did by following the documentation is adding `RC_ICONS = myappico.ico` to the .pro file and i have to run qmake command to make the icon appear . Now it's working just with `app.setWindowIcon(QIcon("room.ico"));` + No ressource file needed :) – The Beast Jan 21 '16 at 01:52
  • Resolved question about doean't working mothod **RC_ICONS +=** http://stackoverflow.com/a/18814639/4149835 – Vladimir Bershov Jan 21 '16 at 09:29
  • It is important to use image path with `QIcon` like this: `app.setWindowIcon(QIcon(":/image.png"));` and NOT image URL like this: `app.setWindowIcon(QIcon("qrc:/image.png"));` notice that image path doesn't have the `qrc` in it – Megidd Oct 28 '18 at 06:43
  • @TheBeast If you are using Windows, RC_ICONS method is sufficient. setWindowIcon function is not needed. Look at the comment in https://stackoverflow.com/a/38660581/10720982 – atakli Mar 31 '23 at 10:05