How to change the desktop icon in Qt?



  • I am very new to Qt. I am trying to find a way to change the desktop icon.
    from this 0_1506984711980_Screenshot from 2017-10-02 15-48-41.png
    to this
    0_1506984720905_statistic-icon.png
    I know there is a official document about this, but it is very confusing. Can anyone give me a step by step guide on how to change the application icon? I am using Linux system.

    Here is what I did.
    I create a folder named icons in the resources folder.
    Copy the image under the icons folder.

    The following is my code
    in main.cpp

    int main(int argc, char** argv)
    {
    
        QApplication app(argc, argv);
        app.setWindowIcon(QIcon(":/statistic-icon.png"));
         ...
    

    icons.qrc

    <!DOCTYPE RCC>
    <RCC version="1.0">
      <qresource>
        <file>icons/statistic-icon.png</file>
      </qresource>
    </RCC>
    

    Thanks in advance.


  • Moderators

    It's easy: You need to create a text file myproject.desktop with the following content:

    [Desktop Entry]
    Version=1.0
    Name=My Project
    Exec=/home/gjiang/myproject/myproject-executable %first_argument
    Terminal=false
    Icon=/home/gjiang/myproject/myproject-icon.png
    Type=Application
    

    Save the file to your desktop folder and that was it :-)



  • What if I install my application in other system? Will the icon remain the same?
    Can I do it programmatically?


  • Moderators

    You need to adjust the file's content depending on the paths where the executable and the icon are stored on the target machine. You will do that programmatically but not with Qt. It depends on your package format (rpm, deb, etc) and the tools you're using to build the packages.



  • I just did this under Linux yesterday. A couple of things to note from @Wieland's solution:

    • It is indeed correct that you need to create a .desktop file to control desktop icon. Qt's setWindowIcon only sets the icon shown for the window, and has no effect on the desktop.
    • To do it "programatically", you need to actually create & write text to that file, through normal file writing code.
    • The icon is purely an external file, it's not embedded in your Qt code. If you move your application elsewhere, you need to take the icon file with it.
    • Relative paths specified in Exec & Icon lines are relative to user's home directory. So you could remove your /home/gjiang/ from each one, which may make it easier to move to a different user/system. (You can not use ~, $HOME or ${HOME} in .desktop file entries.)
    • Under Ubuntu GNOME desktop at least, the default location for user .desktop files is ~/.local/share/applications. I don't know about "Save the file to your desktop folder", but I put mine in that directory. The default location for icons is ~/.local/share/icons, and if you put your icon there your .desktop can set the Icon line to plain myproject-icon.png.
    • On a separate note, if your application cares about what the current directory is (e.g. for reading/writing files), you could add a Path=... line into .desktop file. Again that accepts a relative path, so Path=myproject would put any user in their ~/myproject directory.


  • Thank you so much, Wieland and JNBarchan.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.