Results 1 to 3 of 3

Thread: program exit itself when create dialog with QSystemTrayIcon

  1. #1
    Join Date
    Jul 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question program exit itself when create dialog with QSystemTrayIcon

    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
    Qt Code:
    1. int main(int argc, char* argv[]){
    2. QApplication app(argc, argv);
    3.  
    4. MainWindow *dialog = new MainWindow;
    5.  
    6. return app.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent)
    3. {
    4. setupUi(this);
    5.  
    6. createActions();
    7. createTrayIcon();
    8.  
    9. connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
    10.  
    11. trayIcon->show();
    12.  
    13. showMessage();
    14.  
    15. }
    16.  
    17. void MainWindow::createActions(){
    18. aboutAction = new QAction(tr("&About"), this);
    19. connect(aboutAction, SIGNAL(triggered()), this, SLOT(showAbout()));
    20.  
    21. quitAction = new QAction(tr("&Quit"), this);
    22. connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    23. }
    24.  
    25. void MainWindow::createTrayIcon(){
    26. trayIconMenu = new QMenu(this);
    27. trayIconMenu->addAction(aboutAction);
    28. trayIconMenu->addSeparator();
    29. trayIconMenu->addAction(quitAction);
    30.  
    31. trayIcon = new QSystemTrayIcon(this);
    32. trayIcon->setContextMenu(trayIconMenu);
    33. }
    34.  
    35. void MainWindow::showAbout(){
    36. QMessageBox::about(this, tr("Foo"), tr("Bar"));
    37. }
    38.  
    39. ...
    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

  2. #2
    Join Date
    May 2010
    Posts
    61
    Thanks
    2
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: program exit itself when create dialog with QSystemTrayIcon

    Hi Jimmy,

    Try the following:

    Qt Code:
    1. int main(int argc, char* argv[]){
    2. QApplication app(argc, argv);
    3. QApplication::setQuitOnLastWindowClosed(false);
    4.  
    5. MainWindow *dialog = new MainWindow;
    6.  
    7. return app.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 
    One second is long, everything longer than two seconds is definitely too long.

  3. The following user says thank you to wladek for this useful post:

    hakermania (27th August 2010)

  4. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: program exit itself when create dialog with QSystemTrayIcon

    Thanks for this reply, but if you only add this, then your app doesn't exits at all!
    You have to add QApplication::setQuitOnLastWindowClosed(1); at the CloseEvent
    Anyway thx for the reply, very helpful!

Similar Threads

  1. Multi-dialog program in PyQT will not close
    By Danny Hatt in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2010, 22:12
  2. Non-modal dialog forces app to exit.
    By vereteran in forum Newbie
    Replies: 1
    Last Post: 17th October 2009, 08:39
  3. program a combobox in a dialog
    By Devoraz in forum Newbie
    Replies: 3
    Last Post: 29th July 2009, 10:42
  4. Quit, Exit qApp (program) if error?
    By Arsenic in forum Newbie
    Replies: 13
    Last Post: 30th September 2008, 12:59
  5. Enter key causing program to exit
    By welby in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 16:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.