Search code examples
qtqsystemtrayicon

Application name is displayed with its extension when QSystemTrayIcon's showMessage is used in Windows 10


I'm using QSystemTrayIcon to display notification in Windows 10. Along with the notification, the application name is also displayed. The problem here is the app name is displayed along with the extension(.exe).

Screenshot

How can the extension(.exe) be removed from the notification?


Solution

  • Try adding this line to your .pro file:

    QMAKE_TARGET_DESCRIPTION = "Whatever"
    

    It should change the process name (both in the notifications and in the task manager) to "Whatever".

    More variables like this can be found here: Qmake Variables Documentation

    Note from the documentation:

    This is only utilized if the VERSION or RC_ICONS variable is set and the RC_FILE and RES_FILE variables are not set.


    Step-by-step instructions for creating a test application

    1. Create Qt Widgets Application project, containing a QWidget based widget

    2. Create images directory in the project folder, put an icon file into it (for this example let it be icon.ico)

    3. Add a resource file to the project

    4. To that resource file add prefix /, then "Add Files", selecting ./images/icon.ico

    5. In main.cpp change the code to following:

        #include "widget.h"
        #include <QApplication>
        #include <QIcon>
        
        int main(int argc, char *argv[])
        {
            QCoreApplication::setApplicationName(APP_NAME);
        
            QApplication a(argc, argv);
            a.setWindowIcon(QIcon(":/images/icon.ico"));
        
            Widget w;
            w.setWindowTitle(qApp->applicationName());
            w.setWindowIcon(qApp->windowIcon());
            w.show();
        
            return a.exec();
        }
    
    1. In widget.cpp change code to following:
        #include "widget.h"
        #include "ui_widget.h"
        
        #include <QSystemTrayIcon>
        
        Widget::Widget(QWidget *parent)
            : QWidget(parent)
            , ui(new Ui::Widget)
        {
            ui->setupUi(this);
        
            QSystemTrayIcon *trayIcon = new QSystemTrayIcon(qApp->windowIcon(), this);
            trayIcon->show();
        
            connect(trayIcon, &QSystemTrayIcon::activated, [=]() {
                trayIcon->showMessage("Title", "Message");
            });
        }
        
        Widget::~Widget()
        {
            delete ui;
        }
    
    1. To the bottom of the project (.pro) file add following:
        DEFINES +=  APP_NAME=\\\"AppName\\\"
        
        QMAKE_TARGET_DESCRIPTION = "Whatever"
        
        win32:RC_ICONS += images/icon.ico
    
    1. Save, run qmake (Build -> Run qmake), rebuild project

    2. Start the application. Now the window title should be "AppName", which came from APP_NAME define, both window and tray icon - icon.ico, and the process name in task manager and notifications - "Whatever". You can make the app display a notification by clicking on the tray icon. Notification should look like this:

    screenshot