当我运行此代码并退出输入对话框时,应用程序退出(因此系统托盘图标消失):
的main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.hide();
return a.exec();
}
window.h中
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QSystemTrayIcon>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow();
~MainWindow();
QSystemTrayIcon* sticon;
QAction* actTexte1;
QAction* actTexte3;
public slots:
void changeTexte1();
void changeTexte3();
};
#endif // MAINWINDOW_H
window.cpp
#include "mainwindow.h"
#include <QSystemTrayIcon>
#include <QWidget>
#include <QMenu>
#include <QtGui>
#include <QApplication>
#include <QInputDialog>
MainWindow::MainWindow()
{
sticon = new QSystemTrayIcon(this);
QIcon icon(":/music.png");
sticon->setIcon(icon);
QMenu* stmenu = new QMenu(NULL);
actTexte1 = new QAction("test",NULL);
actTexte3 = new QAction("exit",NULL);
stmenu->addAction(actTexte1);
stmenu->addAction(actTexte3);
sticon->setContextMenu(stmenu);
sticon->show();
connect(actTexte1, SIGNAL(triggered()), this, SLOT(changeTexte1()));
connect(actTexte3, SIGNAL(triggered()), this, SLOT(changeTexte3()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::changeTexte1()
{
bool dialogResult;
QString result = QInputDialog::getText(this, "Echonest API Key", "Enter your API key:", QLineEdit::Normal,
"key", &dialogResult);
}
void MainWindow::changeTexte3()
{
QApplication::quit();
exit(1);
}
如果在主要我设置w.show();而不是w.hide();它没有崩溃但是出现一个空窗口,我不想要这个,我想要一个唯一的系统托盘应用程序。 我该怎么办?