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. Icon for linux application
Qt 6.11 is out! See what's new in the release blog

Icon for linux application

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 8 Posters 8.7k 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.
  • atom_352A atom_352

    Hello, how could i add icon for my app? I need the icon to be displayed in the system tray when launched, I am writing the application for Ubuntu and other Linux systems, my build system is cmake, and the language is C++ (I don’t know if this is important)

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @atom_352 See https://forum.qt.io/topic/102167/how-to-add-qt-application-icon-in-ubuntu

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    atom_352A 1 Reply Last reply
    2
    • jsulmJ jsulm

      @atom_352 See https://forum.qt.io/topic/102167/how-to-add-qt-application-icon-in-ubuntu

      atom_352A Offline
      atom_352A Offline
      atom_352
      wrote on last edited by
      #3

      @jsulm , i tried somthing from that, but probably did something wrong, I think I didn’t really understand where to write this and how to point to an image if it is in .qrc resources

      jsulmJ 1 Reply Last reply
      0
      • atom_352A atom_352

        @jsulm , i tried somthing from that, but probably did something wrong, I think I didn’t really understand where to write this and how to point to an image if it is in .qrc resources

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @atom_352 Did you try QMainWindow::setWindowIcon(), I think this is what you need and you can use an icon embedded in a rosource file.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        atom_352A 1 Reply Last reply
        2
        • jsulmJ jsulm

          @atom_352 Did you try QMainWindow::setWindowIcon(), I think this is what you need and you can use an icon embedded in a rosource file.

          atom_352A Offline
          atom_352A Offline
          atom_352
          wrote on last edited by
          #5

          @jsulm , Yes, I tried to use exactly this, but where can I write this, and is it necessary to use the qicon class?

          Pl45m4P 1 Reply Last reply
          0
          • atom_352A atom_352

            @jsulm , Yes, I tried to use exactly this, but where can I write this, and is it necessary to use the qicon class?

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #6

            @atom_352 said in Icon for linux application:

            but where can I write this

            Anywhere ;-)
            But I don't think your neighbors will like you if you paint that on their door.

            However, to set your MainWindow icon you need to write it in a function that is called when creating your QMainWindow :)

            is it necessary to use the qicon class

            You can construct a QIcon from file, either QRC or directly from file string.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            atom_352A 1 Reply Last reply
            1
            • Pl45m4P Pl45m4

              @atom_352 said in Icon for linux application:

              but where can I write this

              Anywhere ;-)
              But I don't think your neighbors will like you if you paint that on their door.

              However, to set your MainWindow icon you need to write it in a function that is called when creating your QMainWindow :)

              is it necessary to use the qicon class

              You can construct a QIcon from file, either QRC or directly from file string.

              atom_352A Offline
              atom_352A Offline
              atom_352
              wrote on last edited by atom_352
              #7

              @Pl45m4 Thanks!! But should it look like this?

              mainwindow.cpp

              MainWindow::MainWindow(QWidget *parent)
                  : QMainWindow(parent)
                  , ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
                  icon();
              }
              
              void MainWindow::icon()
              {
                   QMainWindow::setWindowIcon(QIcon("qrc:/new/icons/img/information.png"));
              }
              

              Because at startup the taskbar still displays the standard icon

              Pl45m4P 1 Reply Last reply
              0
              • atom_352A atom_352

                @Pl45m4 Thanks!! But should it look like this?

                mainwindow.cpp

                MainWindow::MainWindow(QWidget *parent)
                    : QMainWindow(parent)
                    , ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                    icon();
                }
                
                void MainWindow::icon()
                {
                     QMainWindow::setWindowIcon(QIcon("qrc:/new/icons/img/information.png"));
                }
                

                Because at startup the taskbar still displays the standard icon

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #8

                @atom_352

                This also works

                {
                    ui->setupUi(this);
                    QMainWindow::setWindowIcon(QIcon("qrc:/new/icons/img/information.png"));
                }
                

                Because at startup the taskbar still displays the standard icon

                If you want to have your icon as "shortcut" / desktop icon on Linux you need to follow @JonB 's solution in the topic linked by @jsulm above.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                atom_352A Joe von HabsburgJ 2 Replies Last reply
                1
                • Pl45m4P Pl45m4

                  @atom_352

                  This also works

                  {
                      ui->setupUi(this);
                      QMainWindow::setWindowIcon(QIcon("qrc:/new/icons/img/information.png"));
                  }
                  

                  Because at startup the taskbar still displays the standard icon

                  If you want to have your icon as "shortcut" / desktop icon on Linux you need to follow @JonB 's solution in the topic linked by @jsulm above.

                  atom_352A Offline
                  atom_352A Offline
                  atom_352
                  wrote on last edited by
                  #9

                  @Pl45m4 Ok! Finally, i understand, thank you so much

                  1 Reply Last reply
                  0
                  • atom_352A atom_352 has marked this topic as solved on
                  • S Offline
                    S Offline
                    SimonSchroeder
                    wrote on last edited by
                    #10

                    I am using AppImages (created with linuxdeploy and linuxdeployqt) to have portable applications. These rely on a .desktop file which is supposed to be portable between KDE and Gnome (and hopefully other desktop environments). The .desktop file plays together with the xdg-utils. You can install these with xdg-desktop-menu or xdg-desktop-icon. I'd expect that through this they would also immediately show the icon in the taskbar when launched this way. (You can even register file extensions with xdg-mime and than open file with the associated program with xdg-open–much like macOS's open command.)

                    If I am not mistaken, this is the official link for .desktop files: https://specifications.freedesktop.org/desktop-entry-spec/latest/index.html

                    1 Reply Last reply
                    1
                    • E Offline
                      E Offline
                      eman086
                      wrote on last edited by
                      #11

                      To add a system tray icon in a C++ app on Ubuntu/Linux using CMake, you can use libappindicator or Qt (if using Qt framework). Here's a short summary:

                      Use libappindicator for GTK-based apps:

                      Install: sudo apt install libappindicator3-dev

                      Link in CMake: target_link_libraries(your_app appindicator3-0.1)

                      Set icon: app_indicator_set_icon_full(indicator, "icon-name", "description");

                      Or use Qt:

                      Use QSystemTrayIcon and set icon with setIcon(QIcon(":/icon.png"));

                      Make sure the icon file is included in your build and accessible at runtime.

                      1 Reply Last reply
                      0
                      • Pl45m4P Pl45m4

                        @atom_352

                        This also works

                        {
                            ui->setupUi(this);
                            QMainWindow::setWindowIcon(QIcon("qrc:/new/icons/img/information.png"));
                        }
                        

                        Because at startup the taskbar still displays the standard icon

                        If you want to have your icon as "shortcut" / desktop icon on Linux you need to follow @JonB 's solution in the topic linked by @jsulm above.

                        Joe von HabsburgJ Offline
                        Joe von HabsburgJ Offline
                        Joe von Habsburg
                        wrote on last edited by Joe von Habsburg
                        #12

                        @Pl45m4 said in Icon for linux application:

                        QMainWindow::setWindowIcon(QIcon("qrc:/new/icons/img/information.png"));

                        Hello I have same issue.

                        When I in windows, I use like that

                        //Cmake
                        
                        set(APP_ICON_RESOURCE_WINDOWS "${CMAKE_CURRENT_SOURCE_DIR}/icon.rc")
                        
                        qt_add_executable(MyApp
                            MANUAL_FINALIZATION
                            ${PROJECT_SOURCES}
                            ${APP_ICON_RESOURCE_WINDOWS}
                        )
                        
                        //Icon.rc
                        IDI_ICON1               ICON    "Resources/icon/icon.ico"
                        

                        But I can see only that :
                        5d16d8a8-5629-4b08-b8c3-a3a4d09960c2-image.png

                        I added also on ui,
                        bc1e26b7-c76d-48b7-b900-2440dcd8b322-image.png

                        and extra add with code

                        QMainWindow::setWindowIcon(QIcon("qrc:/new/icons/img/information.png"));
                        

                        Any opinion or advice ?

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          This should work the way you did it. Do you see that rc.exe is executed during compilation? It should create a .res file which then gets linked into the resulting executable

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          0
                          • Joe von HabsburgJ Offline
                            Joe von HabsburgJ Offline
                            Joe von Habsburg
                            wrote on last edited by
                            #14

                            @Christian-Ehrlicher said in Icon for linux application:

                            This should work the way you did it.

                            I did something wrong probably.

                            @Christian-Ehrlicher said in Icon for linux application:

                            Do you see that rc.exe is executed during compilation?

                            I see only that icon : image.png

                            I didn't quite understand what you meant. Are you referring to my application as rx.exe? If so, and by compilation you mean build and run, I don't see it.

                            @Christian-Ehrlicher said in Icon for linux application:

                            It should create a .res file which then gets linked into the resulting executable

                            When I look in the build folder, I don't see any files with a .res extension. Do I need to build it differently?

                            Christian EhrlicherC 1 Reply Last reply
                            0
                            • Joe von HabsburgJ Joe von Habsburg

                              @Christian-Ehrlicher said in Icon for linux application:

                              This should work the way you did it.

                              I did something wrong probably.

                              @Christian-Ehrlicher said in Icon for linux application:

                              Do you see that rc.exe is executed during compilation?

                              I see only that icon : image.png

                              I didn't quite understand what you meant. Are you referring to my application as rx.exe? If so, and by compilation you mean build and run, I don't see it.

                              @Christian-Ehrlicher said in Icon for linux application:

                              It should create a .res file which then gets linked into the resulting executable

                              When I look in the build folder, I don't see any files with a .res extension. Do I need to build it differently?

                              Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              @Joe-von-Habsburg said in Icon for linux application:

                              I didn't quite understand what you meant

                              When you compile the application in QtCreator you see what/how the compiler is invoked. There you see a lot of output and one of them should be the invocation of rc.exe. You need to clean the build dir before or at least modify the rc file so it gets recompiled.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              0
                              • Joe von HabsburgJ Offline
                                Joe von HabsburgJ Offline
                                Joe von Habsburg
                                wrote on last edited by
                                #16

                                I looked at the "Compile Output" window and, it is finished succesfully. I could not see about icon or rc.exe.

                                [15/15 10.2/sec] Linking CXX executable MyApp
                                16:28:18: The command "/home/x/Qt/Tools/CMake/bin/cmake --build /home/x/Desktop/MyApp/build --target all" finished successfully.
                                

                                I want to ask again, is rc.exe my app or different thing ? If different thing how can I add to my app, and why should I add ?
                                I clear cmake configuration, I run cmake, I clean and rebuild. Still icon does not exist.

                                I've worked this hard on Qt in Ubuntu before. I apologize if I did anything wrong.

                                Christian EhrlicherC 1 Reply Last reply
                                0
                                • Joe von HabsburgJ Joe von Habsburg

                                  I looked at the "Compile Output" window and, it is finished succesfully. I could not see about icon or rc.exe.

                                  [15/15 10.2/sec] Linking CXX executable MyApp
                                  16:28:18: The command "/home/x/Qt/Tools/CMake/bin/cmake --build /home/x/Desktop/MyApp/build --target all" finished successfully.
                                  

                                  I want to ask again, is rc.exe my app or different thing ? If different thing how can I add to my app, and why should I add ?
                                  I clear cmake configuration, I run cmake, I clean and rebuild. Still icon does not exist.

                                  I've worked this hard on Qt in Ubuntu before. I apologize if I did anything wrong.

                                  Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  @Joe-von-Habsburg said in Icon for linux application:

                                  I've worked this hard on Qt in Ubuntu before

                                  You're aware that the rc application icon is for windows??? Please read the full thread.

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  1 Reply Last reply
                                  0
                                  • Joe von HabsburgJ Offline
                                    Joe von HabsburgJ Offline
                                    Joe von Habsburg
                                    wrote on last edited by
                                    #18

                                    Ok, I am sorry, I read : https://forum.qt.io/topic/102167/how-to-add-qt-application-icon-in-ubuntu
                                    I need both of them also, .destop file and QMainWindow::setWindowIcon().

                                    I create icon.destop file and add resouces.qrc and add also to build folder.
                                    My file :

                                    [Desktop Entry]
                                    Version=1.0
                                    Type=Application
                                    Name=MyApp
                                    Icon=Resources/icon/icon.png
                                    Exec=MyApp
                                    Comment=MyApp Application
                                    Categories=Application
                                    Terminal=false
                                    

                                    I could not see any thing.
                                    And QMainWindow::setWindowIcon(). not work.
                                    a6acd848-c7f7-4f4b-a406-417ba4fe9b6a-image.png

                                    I checked, is icon null, it is return false. icon is not null.

                                    MainWindow::MainWindow(QWidget *parent)
                                        : QMainWindow(parent)
                                        , ui(new Ui::MainWindow)
                                    {
                                        ui->setupUi(this);
                                        QIcon ic(":/Resources/icon/icon.png");
                                        qDebug() << "ic.isNull" << ic.isNull(); // return false
                                        QMainWindow::setWindowIcon(QIcon(":/Resources/icon/icon.png"));
                                    }
                                    

                                    Did I create .destop file correct location ?

                                    JonBJ 1 Reply Last reply
                                    0
                                    • Joe von HabsburgJ Joe von Habsburg

                                      Ok, I am sorry, I read : https://forum.qt.io/topic/102167/how-to-add-qt-application-icon-in-ubuntu
                                      I need both of them also, .destop file and QMainWindow::setWindowIcon().

                                      I create icon.destop file and add resouces.qrc and add also to build folder.
                                      My file :

                                      [Desktop Entry]
                                      Version=1.0
                                      Type=Application
                                      Name=MyApp
                                      Icon=Resources/icon/icon.png
                                      Exec=MyApp
                                      Comment=MyApp Application
                                      Categories=Application
                                      Terminal=false
                                      

                                      I could not see any thing.
                                      And QMainWindow::setWindowIcon(). not work.
                                      a6acd848-c7f7-4f4b-a406-417ba4fe9b6a-image.png

                                      I checked, is icon null, it is return false. icon is not null.

                                      MainWindow::MainWindow(QWidget *parent)
                                          : QMainWindow(parent)
                                          , ui(new Ui::MainWindow)
                                      {
                                          ui->setupUi(this);
                                          QIcon ic(":/Resources/icon/icon.png");
                                          qDebug() << "ic.isNull" << ic.isNull(); // return false
                                          QMainWindow::setWindowIcon(QIcon(":/Resources/icon/icon.png"));
                                      }
                                      

                                      Did I create .destop file correct location ?

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

                                      @Joe-von-Habsburg said in Icon for linux application:

                                      Icon=Resources/icon/icon.png

                                      QIcon ic(":/Resources/icon/icon.png");

                                      The first looks in the filing system for Resources/icon/icon.png, which doesn't exist. I do not think you can tell desktop to use a resource in a Qt executable, or I don't know how if you can (unless :/Resources/icon/icon.png works which I doubt). For the desktop shortcut I imagine you have to extract or copy the png file so you can point to that.

                                      Joe von HabsburgJ 1 Reply Last reply
                                      0
                                      • JonBJ JonB

                                        @Joe-von-Habsburg said in Icon for linux application:

                                        Icon=Resources/icon/icon.png

                                        QIcon ic(":/Resources/icon/icon.png");

                                        The first looks in the filing system for Resources/icon/icon.png, which doesn't exist. I do not think you can tell desktop to use a resource in a Qt executable, or I don't know how if you can (unless :/Resources/icon/icon.png works which I doubt). For the desktop shortcut I imagine you have to extract or copy the png file so you can point to that.

                                        Joe von HabsburgJ Offline
                                        Joe von HabsburgJ Offline
                                        Joe von Habsburg
                                        wrote on last edited by
                                        #20

                                        @JonB said in Icon for linux application:

                                        Icon=Resources/icon/icon.png

                                        thay are in same directory.

                                        • icon.destop

                                        • Resources/

                                        I added on project location and build location. So it exists.

                                        @JonB said in Icon for linux application:

                                        I do not think you can tell desktop to use a resource in a Qt executable,

                                        I know what you mean. You think I'm using a file in resources.qrc, but I copied Resources/icon/icon.png to the build folder myself. So I know you can't use it from the .qrc file, it's inside the icon.desktop file.

                                        @JonB said in Icon for linux application:

                                        QIcon ic(":/Resources/icon/icon.png");

                                        That was for mainwindow icon. I tested for access to icon.png file or it is null, app can access to file because it returns as false for ask is it null.

                                        JonBJ 1 Reply Last reply
                                        0
                                        • Joe von HabsburgJ Joe von Habsburg

                                          @JonB said in Icon for linux application:

                                          Icon=Resources/icon/icon.png

                                          thay are in same directory.

                                          • icon.destop

                                          • Resources/

                                          I added on project location and build location. So it exists.

                                          @JonB said in Icon for linux application:

                                          I do not think you can tell desktop to use a resource in a Qt executable,

                                          I know what you mean. You think I'm using a file in resources.qrc, but I copied Resources/icon/icon.png to the build folder myself. So I know you can't use it from the .qrc file, it's inside the icon.desktop file.

                                          @JonB said in Icon for linux application:

                                          QIcon ic(":/Resources/icon/icon.png");

                                          That was for mainwindow icon. I tested for access to icon.png file or it is null, app can access to file because it returns as false for ask is it null.

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

                                          @Joe-von-Habsburg
                                          I do not understand your English or what you are saying.

                                          .qrc is not relevant, it is only used at compilation time. Ubuntu/desktop will not look for Icon=Resources/icon/icon.png as an embedded resource within your executable, nor if you specify it as any Icon=:/... like you can inside Qt code.

                                          Let's say your home directory is /home/joe. Copy icon.png to that directory, so /home/joe/icon.png. Specify Icon=/home/joe/icon.png in .desktop file. Try that. If that does not work then perhaps you have something wrong in your .desktop file.

                                          Joe von HabsburgJ 1 Reply Last reply
                                          0

                                          • Login

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