Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: Closing program when widget is closed.

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

    Default Closing program when widget is closed.

    I have a program which instantiates and uses another class (subclass of Qwidget) . When I closed the widget, program does not terminate, how can I also terminate main function?

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Closing program when widget is closed.

    Quote Originally Posted by metdos View Post
    how can I also terminate main function?
    You never need to terminate main function.

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

    Default Re: Closing program when widget is closed.

    In main program, I have a infinite loop, and I need to stop infinite loop when widget is closed by user.

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

    Default Re: Closing program when widget is closed.

    Quote Originally Posted by metdos View Post
    I have a program which instantiates and uses another class (subclass of Qwidget) . When I closed the widget, program does not terminate, how can I also terminate main function?

    means u want to terminate the whole application ...
    use qApp->quit();
    "Behind every great fortune lies a crime" - Balzac

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

    Default Re: Closing program when widget is closed.

    But, how will understand that widget is closed? I put qApp->quit(); to destructor of QT class, but it didn't work.

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

    Default Re: Closing program when widget is closed.

    usually this line
    Qt Code:
    1. ...
    2. QObject::connect( qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()) );
    3. ...
    To copy to clipboard, switch view to plain text mode 
    is added in main.cpp or you can use QApplication::setQuitOnLastWindowClosed
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: Closing program when widget is closed.

    Should I rewrite quit() slot?

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

    Default Re: Closing program when widget is closed.

    why? take a look at QCoreApplication::quit, moreover, you can't do this since this method is not virtual.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: Closing program when widget is closed.

    no just use the connect spirit suggest you ..

    quit() is a static slot
    void QCoreApplication::quit () [static slot]
    "Behind every great fortune lies a crime" - Balzac

  10. #10
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Closing program when widget is closed.

    no need of rewrite.. its like u want to rewrite exit(0)

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

    Default Re: Closing program when widget is closed.

    Then I guess I should try another thing, because it does not work, after widget is closed, my program keeps working.

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

    Default Re: Closing program when widget is closed.

    so, provide a compilable example which reproduces the problem to us.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: Closing program when widget is closed.

    here main function and function which contains infinite loop

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3.  
    4. //GUI part
    5. QApplication a(argc, argv);
    6. rovkonInterface w; //subclass of Qwidget
    7. w.show();
    8.  
    9. QObject::connect( qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()) );
    10.  
    11. userInterface=&w;
    12.  
    13. // start the receive thread
    14. ReceiveThread();
    15.  
    16. return a.exec();
    17.  
    18. }
    19.  
    20. void ReceiveThread()
    21. {
    22.  
    23. rovkonInterface *w=((rovkonInterface *)userInterface);
    24. QApplication *a=((QApplication *)qtApplication);
    25.  
    26.  
    27. while ( lMustQuit == 0 )
    28. {
    29. a->processEvents();
    30.  
    31. }
    32.  
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Closing program when widget is closed.

    if you use another thread then tyr to add one more connection in main.cpp
    Qt Code:
    1. ...
    2. QObject::connect( qApp, SIGNAL(lastWindowClosed()), thread, SLOT(quit()) );
    3. QObject::connect( qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()) );
    4. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: Closing program when widget is closed.

    btw, you can use qApp insted of this QApplication *a=((QApplication *)qtApplication);
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    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

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

    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

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

    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 

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

    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

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

    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].

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.