Results 1 to 10 of 10

Thread: Terminating process after closing QWidget

  1. #1
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Terminating process after closing QWidget

    Hi,

    I have a single QWidget that is defined and showed in the process' main function, like this:
    Qt Code:
    1. // main.cpp
    2.  
    3. #include "main.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication application(argc, argv);
    8. clsWindowMain windowMain();
    9. windowMain.showMaximized();
    10.  
    11. int ret = application.exec();
    12.  
    13. delete(settings);
    14. return ret;
    15. }
    To copy to clipboard, switch view to plain text mode 

    When I run the program and close the QWidget by mouse click, the whole process terminates.

    BUT: When I create a button that should manually close the QWidget like this:
    Qt Code:
    1. void clsWindowMain::slotButtonClicked()
    2. {
    3. this->close();
    4. }
    To copy to clipboard, switch view to plain text mode 
    ... then the QWidget also closes, but the process is still working in background. I can see it when I open the process manager in windows and I can see it when I try to re-run the program...

    Does anybody have a clue why it is not working?

    Thank you in anticipation!

  2. #2
    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: Terminating process after closing QWidget

    We cannot see the code that does any processing so it would be hard to tell you why your processing code does not stop.

  3. #3
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Terminating process after closing QWidget

    Hi Chris,

    you are right. The problem was that my code is really large and I don't know where the problem is so I would have to post all code with all files. I think I'd never get an answer this way...

    Well, I found the problem. What I did not tell you is, that I subclassed QWidget's show-function and called a QDialog in it, this way:
    Qt Code:
    1. void clsWindowMain::show()
    2. {
    3. QWidget::show();
    4. if(!this->windowInstall->isInstalled())
    5. {
    6. this->windowInstall->exec();
    7. if(!this->windowInstall->isInstalled())
    8. this->close();
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    I'd like to let QWidget check whether my program is already installed on the computer or not. If not, then QWidget should automatically call a QDialog that holds all installation settings.
    The problem with it is, that the main event loop has never started (application.exec()), because QDialog has its own event loop and even when I close the QWidget after QDialog terminated, it will return to main function and start the main event loop.

    The problem is, that I can't see a good way to prevent this. How can I create the main QWidget + starting its event loop (application.exec()) and after that automatically checking for installation status? As soon as I start the event loop, I can't do anything till event loop returns...
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication application(argc, argv);
    4.  
    5. clsWindowMain windowMain(settings);
    6. windowMain.show();
    7. int ret = application.exec();
    8.  
    9. if(!windowMain.windowInstall->isInstalled()) // <-- will not work because main function stops and waits at application.exec() till windowMain is closed...
    10. windowMain.windowInstall->exec();
    11.  
    12. return ret;
    13. }
    To copy to clipboard, switch view to plain text mode 

    My only solution is something like that:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication application(argc, argv);
    4.  
    5. clsWindowMain windowMain(settings);
    6. windowMain.show();
    7. windowMain.windowInstall->exec();
    8. int ret;
    9. if(windowMain.windowInstall->isInstalled())
    10. ret = application.exec();
    11.  
    12. return ret;
    13. }
    To copy to clipboard, switch view to plain text mode 
    But I think this method is dirty. I'd like to manage that stuff with methods of windowMain and not manually from the main function...

    How can I show windowMain, start its event loop (application.exec()) and automatically let it check for installation?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Terminating process after closing QWidget

    You call the check method after the event loop is running.
    See QTimer::singleShot()

    Cheers,
    _

  5. #5
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Terminating process after closing QWidget

    You call the check method after the event loop is running.
    See QTimer::singleShot()
    Ahh there we go! Thank you for that hint, it works great!

    There is only one final question left. I do it now like this:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication application(argc, argv);
    4. clsWindowMain windowMain();
    5.  
    6. QTimer::singleShot(2000, &windowMain, SLOT(slotApplicationStart()));
    7. int ret = application.exec();
    8.  
    9. return ret;
    10. }
    11.  
    12. void clsWindowMain::slotApplicationStart()
    13. {
    14. this->showMaximized();
    15. if(!this->windowInstall->isInstalled())
    16. {
    17. this->windowInstall->exec();
    18. if(!this->windowInstall->isInstalled())
    19. this->close();
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    It works, but now I'd like to shorten the time (msec) as much as possible, but I'm afraid of waiting not long enough for application.exec() to finish, so I have the same problem again --> QTimer starts the event loop of QDialog before the main event loop starts...
    What do you think? Is there a way to detect how long application.exec() needs to get started?

    EDIT:
    I realized that even setting QTimer's msec to 1msec does what I wanted... Why this? Does QTimer start multithreading so two event loops can start simultaneously?
    I mean, where is the difference between these to ways:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication application(argc, argv);
    4. clsWindowMain windowMain();
    5.  
    6. QTimer::singleShot(1, &windowMain, SLOT(slotApplicationStart()));
    7. int ret = application.exec();
    8.  
    9. return ret;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication application(argc, argv);
    4. clsWindowMain windowMain();
    5.  
    6. windowMain.slotApplicationStart();
    7. int ret = application.exec();
    8.  
    9. return ret;
    10. }
    To copy to clipboard, switch view to plain text mode 
    The second one fails (logically, because the main event loop did not start)..
    So QTimer starts multithreading??
    Last edited by Binary91; 7th July 2015 at 10:44.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Terminating process after closing QWidget

    Just use time = 0 ms.
    Qt Code:
    1. QTimer::singleShot(0, &windowMain, SLOT(slotApplicationStart()));
    To copy to clipboard, switch view to plain text mode 
    or use QMetaObject::invokeMethod method.

  7. #7
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Terminating process after closing QWidget

    You can use 0 as the interval for the singleShot timer, which will cause the timer to fire as soon as the event loop is running and processed other events that may be pending. i.e. The end of event loop processing checks for timers that may have timed out, so you don't need to guess when the event loop has started, etc.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  8. #8
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Terminating process after closing QWidget

    Ah so a QTimer can't do anything without an event loop? That is what I think after testing a few constellations with qDebug()...
    But is there any difference between the main event loop (application.exec()) and QDialogs event loop? I found out that QTimer is not interested in it, it just starts when any event loop starts...

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Terminating process after closing QWidget

    Quote Originally Posted by Binary91 View Post
    Ah so a QTimer can't do anything without an event loop?
    Exactly.

    Quote Originally Posted by Binary91 View Post
    But is there any difference between the main event loop (application.exec()) and QDialogs event loop?
    No. The dialog's event loop is a "nested event loop", it will temporarily be in control of processing events its "parent" eventloop would otherwise process.
    In this case the parent loop is the main loop.

    Quote Originally Posted by Binary91 View Post
    I found out that QTimer is not interested in it, it just starts when any event loop starts...
    Indeed.

    Cheers,
    _

  10. #10
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Terminating process after closing QWidget

    Great, thank you. So QTimer is exactly the thing for stuff that has to be called after executing an event loop.

    Thank you for your support!

    Cheers,

Similar Threads

  1. Terminating Multiple Running Threads?
    By PLan2010 in forum Newbie
    Replies: 2
    Last Post: 22nd February 2011, 00:15
  2. Terminating a console app
    By cejohnsonsr in forum Newbie
    Replies: 3
    Last Post: 30th August 2010, 22:21
  3. Replies: 1
    Last Post: 18th July 2009, 08:39
  4. Terminating QThread error
    By ramazangirgin in forum Qt Programming
    Replies: 9
    Last Post: 24th August 2008, 19:18
  5. Terminating a thread.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2007, 12:14

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.