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