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 get app icon with name of the app
Forum Updated to NodeBB v4.3 + New Features

how to get app icon with name of the app

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 4.0k Views 2 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.
  • S saber

    i am in linux , qt 5.10.1

    how can i get installed app icon from system with the app name??

    A Offline
    A Offline
    ambershark
    wrote on last edited by ambershark
    #2

    @saber I'm not sure I understand this entirely, but it sounds like you want to find the icon for an application based on the app name. So answering based on that.

    In linux icons are associated with the application via .desktop files. So you would have something like this:

    [Desktop Entry]
    Name=Slack
    Comment=Slack Desktop
    GenericName=Slack Client for Linux
    Exec=env LD_PRELOAD=/usr/lib/libcurl.so.3 /usr/bin/slack %U
    Icon=slack
    Type=Application
    StartupNotify=true
    Categories=GNOME;GTK;Network;InstantMessaging;
    MimeType=x-scheme-handler/slack;
    

    You can see here the application name is Slack and it's icon is set to slack. This slack.desktop file is located in the system default path of /usr/share/applications. If the icon does not have a path (as is this case with this one) it will look in /usr/share/pixmaps for it.

    So if you wanted to find the icon you could grep through /usr/share/applications looking for the .desktop file associated with your application then find the Icon entry. However be aware that there are other places .desktop items can exist, like ~/.local/share/applications, so it's not a foolproof method.

    Also apps in linux do not require a .desktop file to be run so there may not be one at all. In which case they won't have an icon associated with them and will get a default one.

    Oh and not all window managers in linux use .desktop entries. The major ones like KDE and Gnome both do though.

    My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

    S 1 Reply Last reply
    3
    • A ambershark

      @saber I'm not sure I understand this entirely, but it sounds like you want to find the icon for an application based on the app name. So answering based on that.

      In linux icons are associated with the application via .desktop files. So you would have something like this:

      [Desktop Entry]
      Name=Slack
      Comment=Slack Desktop
      GenericName=Slack Client for Linux
      Exec=env LD_PRELOAD=/usr/lib/libcurl.so.3 /usr/bin/slack %U
      Icon=slack
      Type=Application
      StartupNotify=true
      Categories=GNOME;GTK;Network;InstantMessaging;
      MimeType=x-scheme-handler/slack;
      

      You can see here the application name is Slack and it's icon is set to slack. This slack.desktop file is located in the system default path of /usr/share/applications. If the icon does not have a path (as is this case with this one) it will look in /usr/share/pixmaps for it.

      So if you wanted to find the icon you could grep through /usr/share/applications looking for the .desktop file associated with your application then find the Icon entry. However be aware that there are other places .desktop items can exist, like ~/.local/share/applications, so it's not a foolproof method.

      Also apps in linux do not require a .desktop file to be run so there may not be one at all. In which case they won't have an icon associated with them and will get a default one.

      Oh and not all window managers in linux use .desktop entries. The major ones like KDE and Gnome both do though.

      S Offline
      S Offline
      saber
      wrote on last edited by
      #3

      @ambershark

      ok.got that .

      i need to make function to get the icons .
      i am on it.
      thanks.

      D 1 Reply Last reply
      0
      • S saber

        @ambershark

        ok.got that .

        i need to make function to get the icons .
        i am on it.
        thanks.

        D Offline
        D Offline
        Devopia53
        wrote on last edited by
        #4

        @saber

        Have you ever tried this?

        QFileInfo info("/home/myapplication");
        QFileIconProvider   provider;
        QIcon   icon = provider.icon(info);
        
        S 1 Reply Last reply
        3
        • D Devopia53

          @saber

          Have you ever tried this?

          QFileInfo info("/home/myapplication");
          QFileIconProvider   provider;
          QIcon   icon = provider.icon(info);
          
          S Offline
          S Offline
          saber
          wrote on last edited by saber
          #5

          @Devopia53
          sorry for late reply
          i tried it . but not got any icon.

          QFileInfo info("/usr/share/applications/Audacious");
              QFileIconProvider   provider;
              QIcon   icon = provider.icon(info);
              ui->start->setIcon(icon);
          

          qDebug()<< info.isReadable(); is giving me false.But there is icon on that path.

          JonBJ 1 Reply Last reply
          0
          • S saber

            @Devopia53
            sorry for late reply
            i tried it . but not got any icon.

            QFileInfo info("/usr/share/applications/Audacious");
                QFileIconProvider   provider;
                QIcon   icon = provider.icon(info);
                ui->start->setIcon(icon);
            

            qDebug()<< info.isReadable(); is giving me false.But there is icon on that path.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #6

            @saber
            So what does ls -l /usr/share/applications/Audacious show you?

            S 1 Reply Last reply
            0
            • JonBJ JonB

              @saber
              So what does ls -l /usr/share/applications/Audacious show you?

              S Offline
              S Offline
              saber
              wrote on last edited by
              #7

              @JonB

              ls: cannot access '/usr/share/applications/Audacious': No such file or directory

              but there is the desktop file.

              if this is not the way , how to do that properly?

              JonBJ 2 Replies Last reply
              0
              • S saber

                @JonB

                ls: cannot access '/usr/share/applications/Audacious': No such file or directory

                but there is the desktop file.

                if this is not the way , how to do that properly?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #8

                @saber
                So if that file doesn't exist you're not going to get anything good using it.

                qDebug()<< info.isReadable(); is giving me false.But there is icon on that path.
                ...
                but there is the desktop file.

                What do you mean by "But there is icon on that path." ? Where is what desktop file?

                What made you choose to write:
                QFileInfo info("/usr/share/applications/Audacious");
                ?

                1 Reply Last reply
                1
                • S saber

                  @JonB

                  ls: cannot access '/usr/share/applications/Audacious': No such file or directory

                  but there is the desktop file.

                  if this is not the way , how to do that properly?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #9

                  @saber
                  Purely at a guess as to what you're trying to say, combined with what @Devopia53 is trying to suggest, I would say you need to do one of two things.

                  1. Find the .desktop file, which is a text file, and inspect it to see what icon file the desktop uses. Now, this may indeed by in /usr/share/applications, or perhaps ~/.local/share/..., but will be named with a .desktop suffix, e.g. perhaps /usr/share/applications/Audacious.desktop (be careful about capitalization under Linux). Use locate or find to search for suitable file names.

                  2. For @Devopia53's way, find the executable for Audacious. This will not be in /usr/share/applications, but perhaps in /usr/bin or maybe /usr/local/bin or elsewhere. Again use locate or find, or even which. Then try his code. However, personally I am unconvinced this will work under Linux where, unlike Windows, icons are not embedded in executables. But you could try. I think #1 will work instead.

                  A 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @saber
                    Purely at a guess as to what you're trying to say, combined with what @Devopia53 is trying to suggest, I would say you need to do one of two things.

                    1. Find the .desktop file, which is a text file, and inspect it to see what icon file the desktop uses. Now, this may indeed by in /usr/share/applications, or perhaps ~/.local/share/..., but will be named with a .desktop suffix, e.g. perhaps /usr/share/applications/Audacious.desktop (be careful about capitalization under Linux). Use locate or find to search for suitable file names.

                    2. For @Devopia53's way, find the executable for Audacious. This will not be in /usr/share/applications, but perhaps in /usr/bin or maybe /usr/local/bin or elsewhere. Again use locate or find, or even which. Then try his code. However, personally I am unconvinced this will work under Linux where, unlike Windows, icons are not embedded in executables. But you could try. I think #1 will work instead.

                    A Offline
                    A Offline
                    ambershark
                    wrote on last edited by ambershark
                    #10

                    @JonB said in how to get app icon with name of the app:

                    However, personally I am unconvinced this will work under Linux where, unlike Windows, icons are not embedded in executables. But you could try. I think #1 will work instead.

                    Yea it definitely isn't going to work on linux. I even tested it to be sure since I was only about 95% sure it wouldn't work.

                    @saber Icons in linux are not set in the binaries. If you refer to my post above you'll find exactly how to deal with icons in linux. :)

                    QFileIconProvider will not work in linux. You will need to open the file (in your case /usr/share/applications/audacious.desktop) then read it for the Icon line. Keep in mind the Icon line is not guaranteed to be there since icons are not required in linux. Also note, you forgot the .desktop in your path above which is why the file was not found. Also, you used "Audacious" and on my file system it is lower case, i.e. audacious.desktop.

                    And the last thing to be aware of is the Icon entry can be something simple like in Audacious's case, it is:

                    Icon=audacious 
                    

                    There's no extension or path. This will look in /usr/share/pixmaps or other places like /usr/share/icons... which is the case for audacious:

                    /usr/share/icons/hicolor/48x48/apps/audacious.png
                    /usr/share/icons/hicolor/scalable/apps/audacious.svg
                    

                    My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                    S 1 Reply Last reply
                    0
                    • A ambershark

                      @JonB said in how to get app icon with name of the app:

                      However, personally I am unconvinced this will work under Linux where, unlike Windows, icons are not embedded in executables. But you could try. I think #1 will work instead.

                      Yea it definitely isn't going to work on linux. I even tested it to be sure since I was only about 95% sure it wouldn't work.

                      @saber Icons in linux are not set in the binaries. If you refer to my post above you'll find exactly how to deal with icons in linux. :)

                      QFileIconProvider will not work in linux. You will need to open the file (in your case /usr/share/applications/audacious.desktop) then read it for the Icon line. Keep in mind the Icon line is not guaranteed to be there since icons are not required in linux. Also note, you forgot the .desktop in your path above which is why the file was not found. Also, you used "Audacious" and on my file system it is lower case, i.e. audacious.desktop.

                      And the last thing to be aware of is the Icon entry can be something simple like in Audacious's case, it is:

                      Icon=audacious 
                      

                      There's no extension or path. This will look in /usr/share/pixmaps or other places like /usr/share/icons... which is the case for audacious:

                      /usr/share/icons/hicolor/48x48/apps/audacious.png
                      /usr/share/icons/hicolor/scalable/apps/audacious.svg
                      
                      S Offline
                      S Offline
                      saber
                      wrote on last edited by
                      #11

                      @ambershark
                      yes understood the linux icon system.
                      just tried @Devopia53 code if i could find a short solution .

                      @JonB i also tried lower case app name .that gives me a UNKNOWN file icon.
                      in /usr/share/applications/ all the file name are in capital latter
                      0_1527049126913_k.png

                      also i tried this

                          QString name = "chromium";
                          QIcon   icon =  QIcon::fromTheme("applications-" + name.toLower());
                          ui->start->setIcon(icon);
                      

                      this should give me the icon but dose't .

                      this gives me the icon

                          QString name = "chromium";
                          QIcon   icon =  QIcon::fromTheme(name.toLower());
                          ui->start->setIcon(icon);
                      

                      but it gives me the icon not from default theme . it gives the icon from other installed theme.

                      JonBJ A 2 Replies Last reply
                      0
                      • S saber

                        @ambershark
                        yes understood the linux icon system.
                        just tried @Devopia53 code if i could find a short solution .

                        @JonB i also tried lower case app name .that gives me a UNKNOWN file icon.
                        in /usr/share/applications/ all the file name are in capital latter
                        0_1527049126913_k.png

                        also i tried this

                            QString name = "chromium";
                            QIcon   icon =  QIcon::fromTheme("applications-" + name.toLower());
                            ui->start->setIcon(icon);
                        

                        this should give me the icon but dose't .

                        this gives me the icon

                            QString name = "chromium";
                            QIcon   icon =  QIcon::fromTheme(name.toLower());
                            ui->start->setIcon(icon);
                        

                        but it gives me the icon not from default theme . it gives the icon from other installed theme.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #12

                        @saber
                        Sorry, don't understand your problem now.

                        As both I & @ambershark have said, the file you need to look at should be at /usr/share/applications/audacious.desktop, exactly as that is written. It should be text file you can look at which will show you, in its Icon=... line, the path to the icon file used by the "Audacious" application. @ambershark has even shown you the content, naming a .png & a svg path for the icon file. Which I believe is exactly what you are looking for.

                        P.S. You wrote:

                        in /usr/share/applications/ all the file name are in capital latter

                        I think you'll find that's just whatever the application you're showing in your screenshot has chosen to display them as. Try ls /usr/share/applications if you want to know what the filenames actually are!

                        1 Reply Last reply
                        1
                        • S saber

                          @ambershark
                          yes understood the linux icon system.
                          just tried @Devopia53 code if i could find a short solution .

                          @JonB i also tried lower case app name .that gives me a UNKNOWN file icon.
                          in /usr/share/applications/ all the file name are in capital latter
                          0_1527049126913_k.png

                          also i tried this

                              QString name = "chromium";
                              QIcon   icon =  QIcon::fromTheme("applications-" + name.toLower());
                              ui->start->setIcon(icon);
                          

                          this should give me the icon but dose't .

                          this gives me the icon

                              QString name = "chromium";
                              QIcon   icon =  QIcon::fromTheme(name.toLower());
                              ui->start->setIcon(icon);
                          

                          but it gives me the icon not from default theme . it gives the icon from other installed theme.

                          A Offline
                          A Offline
                          ambershark
                          wrote on last edited by ambershark
                          #13

                          @saber said in how to get app icon with name of the app:

                          /usr/share/applications/ all the file name are in capital latter

                          I would double check that whatever file manager you are using there isn't changing them to the names that are inside the .desktop file. I'm pretty sure it is. I've never seen something like Add/Remove Software.desktop on a linux file system. If nothing else using a path symbol (/) in a filename would be just begging for trouble.

                          My bet is if you do the ls /usr/share/applications they will be all (or at least mostly) in lowercase.

                          As for the QIcon::fromTheme stuff. I have no idea. I've never used it. It may work for you but from the sounds of it, it's not.

                          Here's a quick shell command to get the name of the icon file (sorry don't have time to write it up in Qt since I'm on my way out the door):

                          $ cat /usr/share/applications/audacious.desktop | grep Icon | gawk -F= '{ print($NF); }'
                          audacious
                          

                          Then you could even link that to get you the actual filenames:

                          $ find /usr/share/icons -name "$(cat /usr/share/applications/audacious.desktop | grep Icon | gawk -F= '{ print($NF); }')*.png"
                          /usr/share/icons/hicolor/48x48/apps/audacious.png
                          

                          There's a bit more to it than that since they could be full path names in the Icon in the desktop file or they could point to a place like /usr/share/pixmaps. But that's essentially it. You could even shell out to bash and use those commands if you were too lazy to use QFile to read it and find the icon file.

                          Either way, here's the solution in pseudocode:

                          Open filename /usr/share/applications/whatever.desktop
                          Read file looking for line starting with Icon=
                          Parse Icon= to get the filename
                          if filename is absolute or relative path
                             return filename
                          else
                             check /usr/share/icons/* for filename.* (image formats only here, like svg, png)
                             if found
                                return full path + filename
                             else check /usr/share/pixmaps
                             if found
                                 return full path + filename
                          return failure
                          

                          Then once you get a filename, just open it and load it with QIcon.

                          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                          JonBJ 1 Reply Last reply
                          3
                          • A ambershark

                            @saber said in how to get app icon with name of the app:

                            /usr/share/applications/ all the file name are in capital latter

                            I would double check that whatever file manager you are using there isn't changing them to the names that are inside the .desktop file. I'm pretty sure it is. I've never seen something like Add/Remove Software.desktop on a linux file system. If nothing else using a path symbol (/) in a filename would be just begging for trouble.

                            My bet is if you do the ls /usr/share/applications they will be all (or at least mostly) in lowercase.

                            As for the QIcon::fromTheme stuff. I have no idea. I've never used it. It may work for you but from the sounds of it, it's not.

                            Here's a quick shell command to get the name of the icon file (sorry don't have time to write it up in Qt since I'm on my way out the door):

                            $ cat /usr/share/applications/audacious.desktop | grep Icon | gawk -F= '{ print($NF); }'
                            audacious
                            

                            Then you could even link that to get you the actual filenames:

                            $ find /usr/share/icons -name "$(cat /usr/share/applications/audacious.desktop | grep Icon | gawk -F= '{ print($NF); }')*.png"
                            /usr/share/icons/hicolor/48x48/apps/audacious.png
                            

                            There's a bit more to it than that since they could be full path names in the Icon in the desktop file or they could point to a place like /usr/share/pixmaps. But that's essentially it. You could even shell out to bash and use those commands if you were too lazy to use QFile to read it and find the icon file.

                            Either way, here's the solution in pseudocode:

                            Open filename /usr/share/applications/whatever.desktop
                            Read file looking for line starting with Icon=
                            Parse Icon= to get the filename
                            if filename is absolute or relative path
                               return filename
                            else
                               check /usr/share/icons/* for filename.* (image formats only here, like svg, png)
                               if found
                                  return full path + filename
                               else check /usr/share/pixmaps
                               if found
                                   return full path + filename
                            return failure
                            

                            Then once you get a filename, just open it and load it with QIcon.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #14

                            @ambershark
                            Why would you go

                            cat /usr/share/applications/audacious.desktop | grep Icon
                            

                            when you could just go

                            grep Icon /usr/share/applications/audacious.desktop
                            

                            saving a process & a pipe?

                            Also, it's many years since I did an awk (I'm a perl man), but can't you just do for the whole thing:

                            gawk -F= '/^Icon/ { print($NF); }' /usr/share/applications/audacious.desktop
                            

                            saving another process & pipe?

                            :)

                            A 1 Reply Last reply
                            3
                            • JonBJ JonB

                              @ambershark
                              Why would you go

                              cat /usr/share/applications/audacious.desktop | grep Icon
                              

                              when you could just go

                              grep Icon /usr/share/applications/audacious.desktop
                              

                              saving a process & a pipe?

                              Also, it's many years since I did an awk (I'm a perl man), but can't you just do for the whole thing:

                              gawk -F= '/^Icon/ { print($NF); }' /usr/share/applications/audacious.desktop
                              

                              saving another process & pipe?

                              :)

                              A Offline
                              A Offline
                              ambershark
                              wrote on last edited by ambershark
                              #15

                              @JonB Lol you totally could. Using cat is an old habit of mine and I catch myself doing it all the time especially with grep... and same with grep. It could definitely all be done in gawk but usually when I do stuff like that it is to not have to spend time looking at command line help on something like gawk and just using what tool I know off the top of my head. :)

                              Edit: after I wrote this I was copying a file over ssh and throwing it through xz on both ends and I did: cat - | xz -dc - > images/win10-base-setup.qcow2, and immediately laughed when I saw I was using cat - again and only to use - in xz. The whole cat was completely useless. Made me laugh as I thought about this post figured I'd come show ya my bad cat habits in action. ;)

                              My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                              1 Reply Last reply
                              1
                              • S Offline
                                S Offline
                                saber
                                wrote on last edited by
                                #16

                                here is the code

                                // get all the istalled theme
                                    QDirIterator it("/usr/share/icons", QDir::Dirs | QDir::NoDotAndDotDot);
                                    QStringList iconThemes;
                                    while (it.hasNext()) {
                                      it.next();
                                      iconThemes.append(it.fileName());
                                    }
                                
                                // select the first icon theme from the iconThemes list.
                                // i is the app icon
                                QIcon i =QIcon::fromTheme(appName, QIcon::fromTheme(iconThemes.at(1);));
                                
                                

                                thanks

                                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