Results 1 to 5 of 5

Thread: Problem with closeEvent handler implementation.

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Problem with closeEvent handler implementation.

    I'm trying to reimplement a closeEvent handler but is not working as i want. I hope you can help me. I need start a python program as a last "task" of my main program. Here my first attempt.
    Qt Code:
    1. void MainWindow::closeEvent(QCloseEvent *wd) {
    2. QString uploaderPath = "python " + QCoreApplication::applicationDirPath( ) + "/Uploader/setter.py";
    3.  
    4. QProcess *up = new QProcess( this );
    5. up->start( uploaderPath );
    6.  
    7. wd->accept();
    8. }
    To copy to clipboard, switch view to plain text mode 
    Error message when i close my main program:
    QProcess: Destroyed while process ("python") is still running.
    So, i tried to create a second thread where i could run this python program. I thought this program would run regardless of my main program. I close my main program, since the second(python) will run in another thread he will do what he should and will die too. But i was wrong. Here the code.
    Second attempt.
    Qt Code:
    1. #ifndef RESETTER_H
    2. #define RESETTER_H
    3.  
    4. #include <QtCore>
    5.  
    6. class Resetter : public QThread {
    7. public:
    8. Resetter( );
    9. void run( );
    10. };
    11.  
    12. #endif // RESETTER_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "resetter.h"
    2.  
    3. Resetter::Resetter( ) { }
    4.  
    5. void Resetter::run( ) {
    6. QString uploaderPath = "python " + QCoreApplication::applicationDirPath( ) + "/Uploader/setter.py";
    7.  
    8. QProcess *up = new QProcess( this );
    9. up->start( uploaderPath );
    10. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::closeEvent(QCloseEvent *wd) {
    2. Resetter rs;
    3.  
    4. rs.start( );
    5.  
    6. wd->accept();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Error message when i close my main program:
    QThread: Destroyed while thread is still running
    Segmentation fault (core dumped)

    Or create that object on the heap:
    Qt Code:
    1. void MainWindow::closeEvent(QCloseEvent *wd) {
    2. Resetter *rs = new Resetter( );
    3.  
    4. rs->start( );
    5.  
    6. wd->accept();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Error message when i close my main program:
    QCoreApplication::applicationDirPath: Please instantiate the QApplication object first
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QThread(0x112e700), parent's thread is QThread(0x1098e60), current thread is QThread(0x112e700)

  2. #2
    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: Problem with closeEvent handler implementation.

    You can either make the program wait for the process or use QProcess::startDetached().

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    robgeek (18th February 2016)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with closeEvent handler implementation.

    What is likely causing your problem (well, several things, actually) are the facts that you are creating your QProcess instances with the MainWindow instance as parent. This means the QProcess instance will be destroyed when the main window is destroyed. Secondly, QApplication's default setting is to quit when the last top-level window closed. In your case, quitting will ensure that the MainWindow instance is destroyed along with your QProcess. Creating your Resetter instance on the stack is useless, since it will be destroyed as soon as the closeEvent() exits.

    You can try starting your process detached, as anda_skoa suggests, or you can set the quit on last window closed flag to false. Or, maybe you can create a second, non-visible top-level widget that can be the parent of your QProcess. You can connect the QProcess::finished() signal to that widget's close() slot. Other possibilities as well - turn the quit flag off, keep the QProcess as a child of MainWindow, but connect the finished() signal to the app's quit() slot.

  5. #4
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Problem with closeEvent handler implementation.

    Quote Originally Posted by anda_skoa View Post
    You can either make the program wait for the process or use QProcess::startDetached().

    Cheers,
    _
    Its working as i want. Thanks!
    But what's the difference between use QProcess::startDetached() and QThreads as i tried?

  6. #5
    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: Problem with closeEvent handler implementation.

    When using QProcess::startDetached(), an external process that is then detached from the parent process, so that it can persist after the parent process has ended.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Problem with QGraphicsProxyWidget implementation
    By papillon in forum Qt Programming
    Replies: 8
    Last Post: 26th April 2014, 18:00
  2. Replies: 6
    Last Post: 21st March 2011, 14:29
  3. List implementation problem
    By Kanguro in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2009, 19:38
  4. implementation of QabstractItemModel problem
    By mikelantonio in forum Newbie
    Replies: 2
    Last Post: 7th August 2009, 17:31
  5. QAbstractTextDocumentLayout handler problem
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2008, 21:58

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.