Results 1 to 20 of 28

Thread: Closing program when widget is closed.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2009
    Posts
    39
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Closing program when widget is closed.

    Thanks for the "qApp" tip.

    and sorry for the bad names I gave to functions. Actually there is no other thread, there is just a function with infinite loop and I added "a->processEvents();" in order to prevent freezing in widgets as you can see in code above.

    Simply ReceiveThread() continues to run infinite loop when widget is closed, I need to stop this infinite loop.

    Thanks anyway

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: Closing program when widget is closed.

    in rovkonInterface class
    is it a QDialog or QWidget ..
    if QDialog we can receive
    void QDialog::rejected ()
    with that rejected call we can quit our application like

    Qt Code:
    1. QObject::connect( w, SIGNAL(rejected()), qApp, SLOT(quit()) );
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Jul 2009
    Posts
    39
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Closing program when widget is closed.

    It is Qwidget.

    Qt Code:
    1. rovkonInterface w; //subclass of Qwidget
    To copy to clipboard, switch view to plain text mode 


    Quote Originally Posted by wagmare View Post
    in rovkonInterface class
    is it a QDialog or QWidget ..
    if QDialog we can receive
    void QDialog::rejected ()
    with that rejected call we can quit our application like

    Qt Code:
    1. QObject::connect( w, SIGNAL(rejected()), qApp, SLOT(quit()) );
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: Closing program when widget is closed.

    so u try emit your custom signal on rovkonInterface closeEvent()

    reimplement closeEvent() in rovkonInterface() class
    emit widgetClosed() // your custom signal
    and

    QObject::connect( w, SIGNAL(widgetClosed()), qApp, SLOT(quit()) );
    "Behind every great fortune lies a crime" - Balzac

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: Closing program when widget is closed.

    Quote Originally Posted by wagmare View Post
    so u try emit your custom signal on rovkonInterface closeEvent()

    reimplement closeEvent() in rovkonInterface() class
    emit widgetClosed() // your custom signal
    and

    QObject::connect( w, SIGNAL(widgetClosed()), qApp, SLOT(quit()) );
    in that case you don't need to emit a signal, just call qApp->quit(); in closeEvent.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Jul 2009
    Posts
    39
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Closing program when widget is closed.

    didn't work,

    Even I don't understand how closing QT application will terminate main program.

    As long as I understand, I using an instance of QT class, and when user closed the widget, this instance will gone, but how it can effect my other function with infinite loop. So if I can find a way which inform me that instance of Qt class is closed, I think that I can solve my problem.

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: Closing program when widget is closed.

    there is such signal QObject::destroyed.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: Closing program when widget is closed.

    Quote Originally Posted by metdos View Post
    didn't work,

    Even I don't understand how closing QT application will terminate main program.

    As long as I understand, I using an instance of QT class, and when user closed the widget, this instance will gone, but how it can effect my other function with infinite loop. So if I can find a way which inform me that instance of Qt class is closed, I think that I can solve my problem.
    see every Qt application will have QApplication

    from docs:
    For any GUI application that uses Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any time.
    so qApp->quit() will try to close that QApplication .. Tells the application to exit with return code 0 (success).
    "Behind every great fortune lies a crime" - Balzac

  9. #9
    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 

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

    metdos (29th July 2009)

  11. #10
    Join Date
    Jul 2009
    Posts
    39
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Smile Re: Closing program when widget is closed.

    Yes, these are the magical lines I'm searching.

    Qt Code:
    1. loop.isRunning()
    To copy to clipboard, switch view to plain text mode 

    and thanks, it helped very much.

    Quote Originally Posted by numbat View Post
    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 

  12. #11
    Join Date
    Jul 2009
    Posts
    39
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Closing program when widget is closed.

    Again me, yes it helped to stop program, but this time program does not enter infinite loop.
    loop.isRunning() is returning false every time.

    Quote Originally Posted by metdos View Post
    Yes, these are the magical lines I'm searching.

    Qt Code:
    1. loop.isRunning()
    To copy to clipboard, switch view to plain text mode 

    and thanks, it helped very much.

  13. #12
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: Closing program when widget is closed.

    switch to threads ... QThread is the best option here .. no need for u to use
    qApp->processEvents()

    refer this documentation
    http://doc.trolltech.com/qq/qq27-responsive-guis.html
    "Behind every great fortune lies a crime" - Balzac

  14. #13
    Join Date
    Jul 2009
    Posts
    39
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Closing program when widget is closed.

    Very good and neat article.
    Quote Originally Posted by wagmare View Post

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.