#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;
}
答案 0 :(得分:1)
问题是,只要执行存在QSystemTrayIcon
构造函数,MainWindow
就会销毁。你应该这样做:
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 );
}