Application Icon (Qt Quick, Qt 6.2, CMake, Linux)
-
Hi,
I have the problem setting the icon of my Qt Quick App. (Top left corner)
It is a QGuiApplication.
Using Qt 6.2 LTS with CMake on Linux Fedora KDE.
I already have tried multiple "Guides" from the forum but no luck -(
This is what I have:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.16) project(BUL VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOMOC ON) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.2 COMPONENTS Quick REQUIRED) qt_add_executable(appBUL main.cpp ) qt_add_qml_module(appBUL URI BUL VERSION 1.0 QML_FILES main.qml ) set_target_properties(appBUL PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_link_libraries(appBUL PRIVATE Qt6::Quick) install(TARGETS appBUL BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
main.cpp:
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QIcon> // #include <QPalette> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); // app.setWindowIcon(QIcon("/images/logo_square.png")); // app.setWindowIcon(QIcon("/images/logo_square.ico")); // QGuiApplication::setWindowIcon(QIcon("/images/logo_square.png")); // app.setWindowIcon(QtGui.QIcon('/images/logo_square.png')) QQmlApplicationEngine engine; const QUrl url(u"qrc:/BUL/main.qml"_qs); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
main.qml:
import QtQuick import QtQuick.Controls 6.3 // Added via "Design" Version 6.3 ??? ApplicationWindow { // Using ApplicationWindow instead of Window so I can use menuBar width: 1000 height: 800 visible: true title: qsTr("BUL Unofficial Linux") menuBar: MenuBar { Menu { title: qsTr("BUL") Menu { title: qsTr("Accounts") Action { text: qsTr("Online") } Action { text: qsTr("Offline") } } Action { text: qsTr("Settings") } // Action { text: qsTr("&Save") } // Action { text: qsTr("Save &As...") } MenuSeparator { } Action { text: qsTr("Quit") } } Menu { title: qsTr("Color") Action { text: qsTr("Red") } Action { text: qsTr("Green") } Action { text: qsTr("Blue") } } Menu { title: qsTr("Shape") Action { text: qsTr("Round") } Action { text: qsTr("Square") } Action { text: qsTr("Triangle") } } Menu { title: qsTr("Alerts") Action { text: qsTr("Today") } Action { text: qsTr("Yesterday") } } Menu { title: qsTr("Help") Action { text: qsTr("About") } } } }
I have the logo/icon as .png & .ico within a folder "images" but both don't work, also does the pixel size matter ?
Any help or pointing me to correct docs would be very nice -)
Thanks.
-
assuming it is the same in Qt 6.2 as in Qt 5.15
main app icon:
// main.cpp // path to image of .qrc file app.setWindowIcon(QIcon(":/images/logo_square.png"));
for the interactive app icon (e.g. on Ubuntu in the top right corner, on Windows bottom right corner):
// main.qml SystemTrayIcon { id: appIconWithContextMenu visible: true // url to file inside of .qrc icon.source: "qrc:/images/logo_square.png" tooltip: "My App Name" menu: Menu { MenuItem { text: qsTr("Quit") onTriggered: Qt.quit() } MenuItem { text: qsTr("Some Action") onTriggered: { // e.g. trigger a synchronization feature or anything else } } } }
-
To add an icon to your qt quick app just follow these 2 easy steps:
- open the .qrc file where you have added your static images.
If you don't have a qrc file in your project then do the following.
This is the GUI way.
->Open your project in qt creator.
->Add new qt resources file, then open this file in your editor
->Press add files button and search and select your icon.
->Save the qrc file.Text Editor Way
->Open your qrc file in your favorite text editor and add the following lines.<RCC> <qresource prefix="/"> <file>images/logo.png</file> </qresource> </RCC>
Note the qrc file must be added in the CMakeLists.txt file if you are using cmake or .pro file if you are using qmake.
- Open your main.cpp
#include <QGuiApplication> #include <QIcon> int main(int argc, char** argv) { QGuiApplication app(argc, argv); QGuiApplication::setWindowIcon(QIcon(":/images/logo.png")); // next do whatever is needed. }