tomite
tomite

Reputation: 15

Qt c++ QSystemTrayIcon not on tray, windows 7

what am i doing wrong? Program runs and compiles but icon is not on tray.
My OS is Windows 7.

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow( QWidget * parent )
    : QMainWindow( parent )
     , ui( new Ui::MainWindow )
{
    QMenu * trayIconMenu;
    trayIconMenu = new QMenu();
    QSystemTrayIcon tray;
    QIcon icon( ":/ok.png" );
    tray.setIcon( icon );
    tray.setContextMenu( trayIconMenu );
    tray.show();
    ui->setupUi( this );

}

MainWindow::~MainWindow()
{
    delete ui;
}

Upvotes: 0

Views: 684

Answers (1)

vahancho
vahancho

Reputation: 21220

The problem is that your QSystemTrayIcon destroys as soon as the execution exists your MainWindow constructor. You should rather do this:

MainWindow::MainWindow( QWidget * parent )
    : QMainWindow( parent ),
      ui( new Ui::MainWindow )
{
    QMenu * trayIconMenu = new QMenu();
    QSystemTrayIcon *tray = new QSystemTrayIcon(QIcon( ":/ok.png" ), this);
    tray->setContextMenu( trayIconMenu );
    tray->show();
    ui->setupUi( this );    
}

Upvotes: 1

Related Questions