Important: Please read the Qt Code of Conduct -

[SOLVED]QSystemTrayIcon for console applications

  • Hi all,

    Can I use QSystemTrayIcon for console applications?

    I tried to test it by using the following snippet but I got a runtime error:


    #include <QtCore/QCoreApplication>
    #include <QSystemTrayIcon>

    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    QSystemTrayIcon *iconTray = new QSystemTrayIcon(QIcon(":/pixmap/res/icon.png"));
    return a.exec(&#41;;


    QT += core

    QT += gui

    CONFIG += console
    CONFIG += app_bundle

    TEMPLATE = app

    SOURCES += main.cpp


    ---runtime error
    QWidget: Cannot create a QWidget when no GUI is being used

    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.

    [EDIT: fixed code formatting, Volker]

  • You can build a console application in the sense that there is no main window or GUI or the like. But as you instantiate a GUI element - the QSystemTrayIcon - your application needs to initialize the graphics system. Therefore your need a QApplication instead of a QCoreApplication. If you change that, the code should work.

  • Thanks a lot... it's working fine

Log in to reply