How to set an application icon in Qt

10,175

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.

Share:
10,175
The Beast
Author by

The Beast

* BY DAY : work by Nights : HAcks and Movies *

Updated on July 16, 2022

Comments

  • The Beast
    The Beast almost 2 years

    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
    Alexander V over 8 years
    Make sure you suggest the proper string in function argument.
  • Tas
    Tas over 8 years
    @AlexanderVX Ah well spotted, thanks. I've updated the answer.
  • The Beast
    The Beast over 8 years
    @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 :)
  • Vladimir Bershov
    Vladimir Bershov over 8 years
    Resolved question about doean't working mothod RC_ICONS += stackoverflow.com/a/18814639/4149835
  • user3405291
    user3405291 over 5 years
    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