Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to change the desktop icon in Qt?
Forum Updated to NodeBB v4.3 + New Features

How to change the desktop icon in Qt?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gjiang
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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 :-)

      1 Reply Last reply
      4
      • G Offline
        G Offline
        gjiang
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          5
          • JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            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.
            1 Reply Last reply
            5
            • G Offline
              G Offline
              gjiang
              wrote on last edited by
              #6

              Thank you so much, Wieland and JNBarchan.

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved