Hi,
I'm using QT creator in WINDOWS7
My program will exit itself when my MainWindow is hidden and I closed the messagebox created by the system tray icon.
main.cpp
int main(int argc, char* argv[]){
MainWindow *dialog = new MainWindow;
return app.exec();
}
int main(int argc, char* argv[]){
QApplication app(argc, argv);
MainWindow *dialog = new MainWindow;
return app.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
MainWindow
::MainWindow(QWidget *parent
) :{
setupUi(this);
createActions();
createTrayIcon();
trayIcon->show();
showMessage();
}
void MainWindow::createActions(){
aboutAction
= new QAction(tr
("&About"),
this);
connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
quitAction
= new QAction(tr
("&Quit"),
this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}
void MainWindow::createTrayIcon(){
trayIconMenu
= new QMenu(this);
trayIconMenu->addAction(aboutAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon->setContextMenu(trayIconMenu);
}
void MainWindow::showAbout(){
}
...
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
createActions();
createTrayIcon();
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
trayIcon->show();
showMessage();
}
void MainWindow::createActions(){
aboutAction = new QAction(tr("&About"), this);
connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}
void MainWindow::createTrayIcon(){
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(aboutAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
}
void MainWindow::showAbout(){
QMessageBox::about(this, tr("Foo"), tr("Bar"));
}
...
To copy to clipboard, switch view to plain text mode
My program is designed not to have the mainwindow.
Every time I pressed "OK" or "X" on the about dialog, my program exited(with code 0.)
But I want my program to keep running in the background.
Thanks for help,
Jimmy
Bookmarks