Set icon for top left title bar of all windows and taskbar
-
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.proagain. 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.cppWhat 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.
-
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? -
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.proagain. 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.cppWhat 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.
@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.) -
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?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.
-
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.
@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.
-
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.
@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 bothsetIcon()&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?
-
@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.)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?
-

(sorry for the delay in posting screenshot, but it kept saying I had to wait 600 seconds to post)
-
@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 bothsetIcon()&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?
@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...