Results 1 to 20 of 28

Thread: Closing program when widget is closed.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #24
    Join Date
    Jul 2009
    Posts
    139
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    13
    Thanked 59 Times in 52 Posts

    Default Re: Closing program when widget is closed.

    QCoreApplication::quit or QCoreApplication::exit simply stop event processing, so that a call to QCoreApplication::exec will return. In your app, you never get to exec because you are in an infinite loop. You are on the right track with your lMustQuit variable, but you never set it to true. One method would be to connect the lastWindowClosed signal to a custom function and set lMustQuit to be true. Fortunately, we can alternatively use QEventLoop which has a method QEventLoop::isRunning which tells us when exit or quit have been called. Here is a simple example:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include <QEventLoop>
    4.  
    5. void ReceiveThread();
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9.  
    10. //GUI part
    11. QApplication a(argc, argv);
    12. QWidget w; //subclass of Qwidget
    13. w.show();
    14.  
    15. QObject::connect( qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()) );
    16.  
    17. // start the receive thread
    18. ReceiveThread();
    19.  
    20. /* We only get here after quit is called. */
    21. return 0;
    22.  
    23. }
    24.  
    25. void ReceiveThread()
    26. {
    27. QEventLoop loop;
    28.  
    29. while (loop.isRunning()) /* Returns false after exit or quit have been called. */
    30. {
    31. loop.processEvents();
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to numbat for this useful post:

    metdos (29th July 2009)

Similar Threads

  1. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  2. Replies: 3
    Last Post: 17th October 2007, 12:52
  3. closing a widget
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 12th June 2007, 09:57
  4. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19
  5. closing dialog in hidden main Widget
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2006, 10:35

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
  •  
Qt is a trademark of The Qt Company.