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. Set icon for top left title bar of all windows and taskbar

Set icon for top left title bar of all windows and taskbar

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 187 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.
  • W Offline
    W Offline
    why_bother
    wrote last edited by
    #1

    I've been trying to get a custom icon I created to display in the top left corner title bar of all windows in a Qt application I've been writing, and no matter what I try, I cannot get it to display. It will only display the default icon no matter what I do. I created a test app to try and just get it to work without doing it in my app, but even that won't work.
    I can get it to display the same icon within a window with QPixmap, so it's not a path issue. And as far as I can tell, it is finding my icon file and embedding it in the executable generated by the Makefile that is generated by qmake. It's just not displaying it, and is displaying the default/generic Qt icon instead.

    I've looked at https://doc.qt.io/qt-6/appicon.html that mentions setIcon() and I'm totally confused, as I don't understand what the difference between setIcon() and setWindowIcon() is, let alone which one I'm supposed to use.

    I've even tried deleting the Makefile created by qmake and the .qmake.stash files and running

    qmake test_icon.pro
    

    again. But it still will not display my icon no matter what I try.

    I'm new to programming with Qt, so I have no idea what I'm doing wrong.

    I'm using Manjaro Linux with Qt5.

    My main.cpp file:

        #include <QApplication>
        #include <QMainWindow>
        #include <QIcon>
         
        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
         
            // Set the application icon using the resource path (:/myicon.png)
            app.setWindowIcon(QIcon(":/myicon.png"));
         
            // Create main window (and any other windows)
            QMainWindow mainWindow;
            mainWindow.setWindowTitle("My App");
            mainWindow.show();
         
            // Any other top-level windows you create will automatically use this icon
            // unless you set a different icon on them individually using setWindowIcon()
            // on the widget instance itself.
         
            return app.exec();
        }
    

    My resources.qrc file:

        <!DOCTYPE RCC>
        <RCC version="1.0">
        <qresource prefix="/">
            <file>myicon.png</file>
        </qresource>
        </RCC>
    

    My test_icon.pro file:

        ######################################################################
        # Automatically generated by qmake (3.1) Fri Jan 16 16:35:33 2026
        ######################################################################
         
        TEMPLATE = app
        TARGET = test_icon
        QT += widgets
        INCLUDEPATH += .
        RESOURCES += resources.qrc
         
        # You can make your code fail to compile if you use deprecated APIs.
        # In order to do so, uncomment the following line.
        # Please consult the documentation of the deprecated API in order to know
        # how to port your code away from it.
        # You can also select to disable deprecated APIs only up to a certain version of Qt.
        #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
         
        # Input
        SOURCES += main.cpp
    

    What in the f**king hell am I missing?? I don't understand what I'm doing wrong. I'm at my wits end with this.
    Can someone please tell me what I'm doing wrong in plain simple english? As clearly I'm too stupid to know what I'm doing wrong, or not doing that I should be doing/missing. I have no idea what I'm supposed to do at this point. Surely there must be a way to do this.

    I want to do this in code and not in Qt Creator, so please don't suggest I use Qt Creator. I'm not interested in getting it to work in Windows at this point. I just want to get this done in Linux at this point.

    Thanks in advance for any help.

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

      This is working fine for me on windows with Qt6.11:

      int main(int argc, char* argv[])
      {
          QApplication a(argc, argv);
          a.setWindowIcon(a.style()->standardIcon(QStyle::SP_MediaPlay));
          QMainWindow mw;
          mw.show();
          return a.exec();
      }
      

      If this works also for you I would guess the resource file is not properly embedded. What does QFile::exists(":/myicon.png") say?

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

      W 1 Reply Last reply
      2
      • W why_bother

        I've been trying to get a custom icon I created to display in the top left corner title bar of all windows in a Qt application I've been writing, and no matter what I try, I cannot get it to display. It will only display the default icon no matter what I do. I created a test app to try and just get it to work without doing it in my app, but even that won't work.
        I can get it to display the same icon within a window with QPixmap, so it's not a path issue. And as far as I can tell, it is finding my icon file and embedding it in the executable generated by the Makefile that is generated by qmake. It's just not displaying it, and is displaying the default/generic Qt icon instead.

        I've looked at https://doc.qt.io/qt-6/appicon.html that mentions setIcon() and I'm totally confused, as I don't understand what the difference between setIcon() and setWindowIcon() is, let alone which one I'm supposed to use.

        I've even tried deleting the Makefile created by qmake and the .qmake.stash files and running

        qmake test_icon.pro
        

        again. But it still will not display my icon no matter what I try.

        I'm new to programming with Qt, so I have no idea what I'm doing wrong.

        I'm using Manjaro Linux with Qt5.

        My main.cpp file:

            #include <QApplication>
            #include <QMainWindow>
            #include <QIcon>
             
            int main(int argc, char *argv[])
            {
                QApplication app(argc, argv);
             
                // Set the application icon using the resource path (:/myicon.png)
                app.setWindowIcon(QIcon(":/myicon.png"));
             
                // Create main window (and any other windows)
                QMainWindow mainWindow;
                mainWindow.setWindowTitle("My App");
                mainWindow.show();
             
                // Any other top-level windows you create will automatically use this icon
                // unless you set a different icon on them individually using setWindowIcon()
                // on the widget instance itself.
             
                return app.exec();
            }
        

        My resources.qrc file:

            <!DOCTYPE RCC>
            <RCC version="1.0">
            <qresource prefix="/">
                <file>myicon.png</file>
            </qresource>
            </RCC>
        

        My test_icon.pro file:

            ######################################################################
            # Automatically generated by qmake (3.1) Fri Jan 16 16:35:33 2026
            ######################################################################
             
            TEMPLATE = app
            TARGET = test_icon
            QT += widgets
            INCLUDEPATH += .
            RESOURCES += resources.qrc
             
            # You can make your code fail to compile if you use deprecated APIs.
            # In order to do so, uncomment the following line.
            # Please consult the documentation of the deprecated API in order to know
            # how to port your code away from it.
            # You can also select to disable deprecated APIs only up to a certain version of Qt.
            #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
             
            # Input
            SOURCES += main.cpp
        

        What in the f**king hell am I missing?? I don't understand what I'm doing wrong. I'm at my wits end with this.
        Can someone please tell me what I'm doing wrong in plain simple english? As clearly I'm too stupid to know what I'm doing wrong, or not doing that I should be doing/missing. I have no idea what I'm supposed to do at this point. Surely there must be a way to do this.

        I want to do this in code and not in Qt Creator, so please don't suggest I use Qt Creator. I'm not interested in getting it to work in Windows at this point. I just want to get this done in Linux at this point.

        Thanks in advance for any help.

        B Offline
        B Offline
        Bonnie
        wrote last edited by Bonnie
        #3

        @why_bother said in Set icon for top left title bar of all windows and taskbar:

        I've looked at https://doc.qt.io/qt-6/appicon.html that mentions setIcon() and I'm totally confused, as I don't understand what the difference between setIcon() and setWindowIcon() is, let alone which one I'm supposed to use.

        setIcon() is for QWindow, which we usually don't use directly.
        setWindowIcon() is for QApplication, it will call QWindow::setIcon() internally for every window.

        As for the linked documentation, it is actually for changing the icon of the executable file, so it need to be configured in the project file, instead of in the code, and need to be certain icon format, instead of any picture format.
        When the executable file icon is set successfully, it should become the default window icon for QApplication, so there would be no need to call QApplication::setWindowIcon() or QWindow::setIcon() if you only have one icon to show. (This is my experience on Windows, not sure if Linux is the same case.)

        W 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          This is working fine for me on windows with Qt6.11:

          int main(int argc, char* argv[])
          {
              QApplication a(argc, argv);
              a.setWindowIcon(a.style()->standardIcon(QStyle::SP_MediaPlay));
              QMainWindow mw;
              mw.show();
              return a.exec();
          }
          

          If this works also for you I would guess the resource file is not properly embedded. What does QFile::exists(":/myicon.png") say?

          W Offline
          W Offline
          why_bother
          wrote last edited by
          #4

          @Christian-Ehrlicher

          QFile::exists() does find it.

          But even trying your example, it still shows the default icon. The qrc_resources.cpp source file is being generated.

          So I don't understand why it will not show. What's the problem?
          Again, there must be a way to do this and I'm once again not interested in Windows, so can we please just forget about Windows.

          I posted a screenshot of what I'm talking about below.

          Christian EhrlicherC JonBJ 2 Replies Last reply
          0
          • W why_bother

            @Christian-Ehrlicher

            QFile::exists() does find it.

            But even trying your example, it still shows the default icon. The qrc_resources.cpp source file is being generated.

            So I don't understand why it will not show. What's the problem?
            Again, there must be a way to do this and I'm once again not interested in Windows, so can we please just forget about Windows.

            I posted a screenshot of what I'm talking about below.

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

            @why_bother said in Set icon for top left title bar of all windows and taskbar:

            So I don't understand why it will not show. What's the problem?

            Since this icon is only a hint to the window manager - the problem is the window manager not honoring this hint.

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

            W 1 Reply Last reply
            0
            • W why_bother

              @Christian-Ehrlicher

              QFile::exists() does find it.

              But even trying your example, it still shows the default icon. The qrc_resources.cpp source file is being generated.

              So I don't understand why it will not show. What's the problem?
              Again, there must be a way to do this and I'm once again not interested in Windows, so can we please just forget about Windows.

              I posted a screenshot of what I'm talking about below.

              JonBJ Online
              JonBJ Online
              JonB
              wrote last edited by JonB
              #6

              @why_bother
              The first thing in your situation is to temporarily get rid of trying to use a resource. Put the icon in an external file at fixed location, verify it's good and pass the full, absolute path when creating the icon. Now you don't have to worry about resources, paths or build procedures. You can also use both setIcon() & setWindowIcon() so you don't have to wonder which one is appropriate while you test. That is how I approach a situation like this. I assume that does not resolve for you?

              Which then perhaps leaves you as @Christian-Ehrlicher says: perhaps your Manjaro or its window manager just does not do what you want? Do you have a choice of window managers/interfaces --- like xcb or wayland or whatever desktop--- which you could check?

              Christian EhrlicherC W 2 Replies Last reply
              0
              • B Bonnie

                @why_bother said in Set icon for top left title bar of all windows and taskbar:

                I've looked at https://doc.qt.io/qt-6/appicon.html that mentions setIcon() and I'm totally confused, as I don't understand what the difference between setIcon() and setWindowIcon() is, let alone which one I'm supposed to use.

                setIcon() is for QWindow, which we usually don't use directly.
                setWindowIcon() is for QApplication, it will call QWindow::setIcon() internally for every window.

                As for the linked documentation, it is actually for changing the icon of the executable file, so it need to be configured in the project file, instead of in the code, and need to be certain icon format, instead of any picture format.
                When the executable file icon is set successfully, it should become the default window icon for QApplication, so there would be no need to call QApplication::setWindowIcon() or QWindow::setIcon() if you only have one icon to show. (This is my experience on Windows, not sure if Linux is the same case.)

                W Offline
                W Offline
                why_bother
                wrote last edited by
                #7

                @Bonnie

                Thank you Bonnie.

                So to be clear; I use setWindowIcon() to set the icon for the icon at the top left side of the window title bar? Please just answer yes or no. And if no, then how exactly do I do it?

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  why_bother
                  wrote last edited by
                  #8

                  test_icon.png

                  (sorry for the delay in posting screenshot, but it kept saying I had to wait 600 seconds to post)

                  I 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @why_bother
                    The first thing in your situation is to temporarily get rid of trying to use a resource. Put the icon in an external file at fixed location, verify it's good and pass the full, absolute path when creating the icon. Now you don't have to worry about resources, paths or build procedures. You can also use both setIcon() & setWindowIcon() so you don't have to wonder which one is appropriate while you test. That is how I approach a situation like this. I assume that does not resolve for you?

                    Which then perhaps leaves you as @Christian-Ehrlicher says: perhaps your Manjaro or its window manager just does not do what you want? Do you have a choice of window managers/interfaces --- like xcb or wayland or whatever desktop--- which you could check?

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

                    @JonB said in Set icon for top left title bar of all windows and taskbar:

                    The first thing in your situation is to temporarily get rid of trying to use a resource.

                    Please see his last post. Even my testcase with an internal icon does not work. It's the wm...

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

                    JonBJ 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @JonB said in Set icon for top left title bar of all windows and taskbar:

                      The first thing in your situation is to temporarily get rid of trying to use a resource.

                      Please see his last post. Even my testcase with an internal icon does not work. It's the wm...

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote last edited by JonB
                      #10

                      @Christian-Ehrlicher
                      Yes you may well be right. My post was really intended for OP to have tested earlier, so that next time they might break a similar problem down before getting lost in considerations which could be eliminated.

                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @why_bother
                        The first thing in your situation is to temporarily get rid of trying to use a resource. Put the icon in an external file at fixed location, verify it's good and pass the full, absolute path when creating the icon. Now you don't have to worry about resources, paths or build procedures. You can also use both setIcon() & setWindowIcon() so you don't have to wonder which one is appropriate while you test. That is how I approach a situation like this. I assume that does not resolve for you?

                        Which then perhaps leaves you as @Christian-Ehrlicher says: perhaps your Manjaro or its window manager just does not do what you want? Do you have a choice of window managers/interfaces --- like xcb or wayland or whatever desktop--- which you could check?

                        W Offline
                        W Offline
                        why_bother
                        wrote last edited by
                        #11

                        @JonB

                        While Christian is correct; I already did try it without using the resource file and it made no difference whatsoever.

                        So no, it doesn't resolve anything.

                        JonBJ 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @why_bother said in Set icon for top left title bar of all windows and taskbar:

                          So I don't understand why it will not show. What's the problem?

                          Since this icon is only a hint to the window manager - the problem is the window manager not honoring this hint.

                          W Offline
                          W Offline
                          why_bother
                          wrote last edited by
                          #12

                          @Christian-Ehrlicher

                          I don't understand what you mean by "hint". What does that mean? I don't understand your comment at all.

                          In any case, I know it is embedding my icon in the executable (using the resource file), so that isn't the problem. But that said, since I'm using KDE and as far as I know KDE uses Qt itself, and all other graphical apps I have installed do have their icons show up in the top left hand corner of the their window title bar; I don't understand why it's not working in my app (or even in a simple test case as above)? I can get it to display the same icon in a QMessageBox using QPixmap(). So again, I don't understand.

                          Without trying to be rude, can you just tell me what in the hell I have to do to get the bloody thing to actually display in the top left corner of the window's title bar (for all windows)?

                          Again, I'm new to programming with Qt, so I really have no idea how to get this bloody thing to just display the icon in the top left corner of the window's title bar. Can I just have a simple example of what I need to do? I literally have no idea what to do next.

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • W why_bother

                            @Christian-Ehrlicher

                            I don't understand what you mean by "hint". What does that mean? I don't understand your comment at all.

                            In any case, I know it is embedding my icon in the executable (using the resource file), so that isn't the problem. But that said, since I'm using KDE and as far as I know KDE uses Qt itself, and all other graphical apps I have installed do have their icons show up in the top left hand corner of the their window title bar; I don't understand why it's not working in my app (or even in a simple test case as above)? I can get it to display the same icon in a QMessageBox using QPixmap(). So again, I don't understand.

                            Without trying to be rude, can you just tell me what in the hell I have to do to get the bloody thing to actually display in the top left corner of the window's title bar (for all windows)?

                            Again, I'm new to programming with Qt, so I really have no idea how to get this bloody thing to just display the icon in the top left corner of the window's title bar. Can I just have a simple example of what I need to do? I literally have no idea what to do next.

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

                            @why_bother said in Set icon for top left title bar of all windows and taskbar:

                            I don't understand what you mean by "hint". What does that mean? I don't understand your comment at all.

                            The window decoration is part of the window manager, Qt just can say it wants an icon or e.g. the close button upper left. But if the window manager ignores this Qt can not do anything against.

                            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
                            • W why_bother

                              @JonB

                              While Christian is correct; I already did try it without using the resource file and it made no difference whatsoever.

                              So no, it doesn't resolve anything.

                              JonBJ Online
                              JonBJ Online
                              JonB
                              wrote last edited by
                              #14

                              @why_bother said in Set icon for top left title bar of all windows and taskbar:

                              So no, it doesn't resolve anything.

                              I know that. It was intended as a helpful suggestion for the future, as I know you said you are new.

                              1 Reply Last reply
                              0
                              • W why_bother

                                test_icon.png

                                (sorry for the delay in posting screenshot, but it kept saying I had to wait 600 seconds to post)

                                I Online
                                I Online
                                IgKh
                                wrote last edited by
                                #15

                                @why_bother Ah you are on Wayland. Wayland does not allow windows to set their own icons, because security or something.

                                There is an experimental upcoming extension protocol for setting window icons from within the application which is not widely implemented. Until then, any API you can call will do exactly nothing.

                                The correct way to set a window icon under Wayland, is to install a desktop file with the 'Icon' key set to a name of an installed icon, and make sure the compositor knows how to associate your app with the desktop file. It is usually automatic for installed applications, but you might need to fiddle with QGuiApplication::setDesktopFileName.

                                The orange W icon is the stock fallback most compositors use when they can't figure out the right desktop entry.

                                I 1 Reply Last reply
                                0
                                • I IgKh

                                  @why_bother Ah you are on Wayland. Wayland does not allow windows to set their own icons, because security or something.

                                  There is an experimental upcoming extension protocol for setting window icons from within the application which is not widely implemented. Until then, any API you can call will do exactly nothing.

                                  The correct way to set a window icon under Wayland, is to install a desktop file with the 'Icon' key set to a name of an installed icon, and make sure the compositor knows how to associate your app with the desktop file. It is usually automatic for installed applications, but you might need to fiddle with QGuiApplication::setDesktopFileName.

                                  The orange W icon is the stock fallback most compositors use when they can't figure out the right desktop entry.

                                  I Online
                                  I Online
                                  IgKh
                                  wrote last edited by
                                  #16

                                  @IgKh said in Set icon for top left title bar of all windows and taskbar:

                                  There is an experimental upcoming extension protocol for setting window icons from within the application which is not widely implemented

                                  Just for completeness, the protocol is xdg-toplevel-icon-v1. In is implemented in KDE's Wayland compositor starting 6.3 and in Qt starting version 6.9. Under a compatible version combination, setWindowIcon works also under Wayland. Since OP is using Qt 5, that is not the case.

                                  It should be further noted that some desktop environments (notably Gnome), force applications to use client-side decorations. When Qt is drawing its' own window decoration, the icon set via setWindowIcon will appear on the window frame, but won't appear elsewhere - overview screen, dock, Alt+Tab switcher, etc.

                                  Therefore specifying a static icon via a desktop file is still the way to go unfortunately.

                                  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