Results 1 to 5 of 5

Thread: My app does not close

  1. #1
    Join Date
    Mar 2011
    Location
    Havana, Cuba
    Posts
    17
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default My app does not close

    Hello people, I have a little problem with an application here, I need to load some data from a file and it works out, but I call the slot init() int the constructor method, so, if the file does not exists I throw an exception and show a QMessageBox, after that I try to close the app but it does not work. Here is the code:

    cpp file

    Qt Code:
    1. ClassName::ClassName( args) {
    2. ...
    3. // some code here
    4. ...
    5. this->init();
    6. }
    7.  
    8. void ClaseName::init() {
    9.  
    10. try{
    11. this->loadFileContent();
    12. }
    13. catch(Error *e) {
    14. QmessageBox:: ... // here show the message
    15. this->close();
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    It show the QMessageBox before the main window, but does not close the app.

    In another slot that I use to write into the file I make the same and if the file does not exists or there is another problem it show the QMessageBox and close the app, but here it does not work.

    Somebody have any suggestion ??

  2. #2
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: My app does not close

    Please provide, declaration of this class.
    What do this close?
    Last edited by Nickolay; 19th September 2011 at 16:26.

  3. #3
    Join Date
    Apr 2011
    Posts
    39
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: My app does not close

    you are suppose to write,

    your application's object instead of this.

    followed by "->close".

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: My app does not close

    If this is what your program looks like:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MainWindow: public QMainWindow
    4. {
    5. public:
    6. MainWindow(QWidget *p=0): QMainWindow(p) {
    7. init();
    8. }
    9.  
    10. void init() {
    11. close();
    12. }
    13.  
    14. };
    15.  
    16. int main(int argc, char **argv)
    17. {
    18. QApplication app(argc, argv);
    19.  
    20. MainWindow w;
    21. w.show();
    22.  
    23. app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

    You call init() in the constructor, the widget has not been displayed yet, you have never reached the main event loop, so close() is meaningless. The constructor completes either way, the next line of your code is widget->show(), which dutifully gets executed and then you fall through in the main event loop. QCoreApplication::exit() doesn't work in that position either.

    You could delay init() until the event loop is reached, or call it explicitly and have it return a bool indicting success/failure which decides whether to continue into the main even loop or not. Without more of your code we can only guess.
    Last edited by ChrisW67; 20th September 2011 at 07:49. Reason: updated contents

  5. The following user says thank you to ChrisW67 for this useful post:

    saimel (21st September 2011)

  6. #5
    Join Date
    Mar 2011
    Location
    Havana, Cuba
    Posts
    17
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: My app does not close

    thanks for the tips, I added the line w.init(); after the w.show() in the main event loop and also removed the line this.init() inside the constructor method and its work out.

Similar Threads

  1. midi child does not close when I call close()
    By qlands in forum Qt Programming
    Replies: 7
    Last Post: 29th July 2011, 22:25
  2. QT application not close after close the mainwindow
    By artome in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2011, 22:23
  3. Replies: 2
    Last Post: 6th May 2011, 08:02
  4. Replies: 2
    Last Post: 17th December 2010, 19:01
  5. Close Button
    By Lycus HackerEmo in forum Newbie
    Replies: 4
    Last Post: 30th December 2009, 05:27

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.