Results 1 to 13 of 13

Thread: qt window close but process still running

  1. #1
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    5

    Unhappy qt window close but process still running

    I have tried with the lastWindowClosed() but still the while loop is running although the window is close.
    here is some parts of my code;

    QTMainForm::QTMainForm(QWidget* parent)
    {
    btn.setText("start");
    btn2.setText("stop");
    connect(&btn, SIGNAL(clicked()), this, SLOT(OnBtnstrt()));
    progress.setValue(0);

    progress.setRange(0, 1000000);
    progress.setWindowTitle(tr("Find Files"));
    vb.addWidget(&progress);
    vb.addWidget(&btn);
    vb.addWidget(&btn2);
    setLayout(&vb);
    }

    void QTMainForm::OnBtnstrt()
    {

    for (int i = 0; i < 10000000; i++)
    {
    progress.setValue(i);
    qApp->processEvents();
    timer.setInterval(1000);
    if(btn2.isDown())
    {

    break;

    }

    progress.show();
    }
    }

    int main(int argc, char *argv[])
    {

    QTMainForm* pMainForm = 0;

    QApplication app(argc, argv);
    app.setObjectName("client");
    app.setQuitOnLastWindowClosed(true);
    pMainForm = new QTMainForm();
    pMainForm->resize(300, 300);
    pMainForm->show();
    pMainForm->setAttribute(Qt::WA_QuitOnClose);
    QObject::connect( qApp, SIGNAL( lastWindowClosed() ),&app, SLOT( setLastWinClosed() ) );
    return app.exec();
    }

    any help will be really appreciated.
    Last edited by sazzad08; 31st December 2012 at 08:08.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: qt window close but process still running

    this is a big number 10000000 for loop are you sure of this
    Last edited by Santosh Reddy; 31st December 2012 at 07:50. Reason: updated contents
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    sazzad08 (31st December 2012)

  4. #3
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    5

    Default Re: qt window close but process still running

    yes. it does. but the problem is when I close the gui window, the process still runs. any help on that?

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: qt window close but process still running

    void QTMainForm::OnBtnstrt()
    {

    pm = new QTMainForm();
    btn.isHidden();
    You are creating a new QTMainForm, what is this for. (I mean you already have one)
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    sazzad08 (31st December 2012)

  7. #5
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    5

    Default Re: qt window close but process still running

    previously, I was trying some code to stop the "while" loop when the window is close. but it didnt work. but i have already taken out them and now i have QTMaiinForm inside the int main(). I complied but still the same problem.

  8. #6
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qt window close but process still running

    You shouldn't use isDown() on a button. Connect a slot to the button and in that slot, you set a stop condition to true.
    Something like pleaseStop = true;

    Your loop will become something like
    while (!pleaseStop)
    {
    Whatever..
    }.

    Don't forget to initialize the stop condition before starting the loop.

  9. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: qt window close but process still running

    Qt Code:
    1. void QTMainForm::OnBtnstrt()
    2. {
    3.  
    4. for (int i = 0; i < 10000000; i++)
    5. {
    6. progress.setValue(i);
    7. qApp->processEvents();
    8. timer.setInterval(1000);
    9. if(btn2.isDown()) // <<<<<<<<<<<<<<<<<<
    10. {
    11.  
    12. break;
    13.  
    14. }
    15.  
    16. progress.show(); // <<<<<<<<<<<<<<<<<<
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    Are you able to see progress bar?
    Where is timer code?
    I think it is not good to check for btn2.isDown()


    I think you are complicating things, here are few tips which you can try.

    Comment out the button click slot and see app exits
    Remove the timer stuff, see it helps
    also the last window closed signal and slot connetion is redudant (it is default behaviour)
    Last edited by Santosh Reddy; 31st December 2012 at 08:37.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    sazzad08 (31st December 2012)

  11. #8
    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: qt window close but process still running

    In your constructor, you aren't calling the base class constructor. If your QTMainForm has a parent, this means that the proper parent-child relationship is not being set up.

    Since you don't give a header file, it is impossible to know what class QTMainForm is derived from, so I will assume it is QDialog for the purpose of the example:

    Qt Code:
    1. QTMainForm::QTMainForm(QWidget* parent)
    2. : QDialog( parent ) // <---- you should always do this, even if "parent" is NULL
    3. {
    4. btn.setText("start");
    5. btn2.setText("stop");
    6. connect(&btn, SIGNAL(clicked()), this, SLOT(OnBtnstrt()));
    7. progress.setValue(0);
    8.  
    9. progress.setRange(0, 1000000);
    10. progress.setWindowTitle(tr("Find Files"));
    11. vb.addWidget(&progress);
    12. vb.addWidget(&btn);
    13. vb.addWidget(&btn2);
    14. setLayout(&vb);
    15. }
    To copy to clipboard, switch view to plain text mode 

    Please learn about "CODE" tags and use them next time you post source code. (Click "Go Advanced" then click the "#" icon on the toolbar. This inserts a pair of CODE tags. Paste your source code in between them). This will also preserve any indentation in your source file, which will make your code easier to read.

  12. #9
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    5

    Default Re: qt window close but process still running

    ok, I have changed my code and it is like this now;

    QTMainForm::QTMainForm(QWidget* parent)
    {

    btn.setText("Start");
    connect(&btn, SIGNAL(clicked()), this, SLOT(OnBtnstrt()));
    progress.setValue(0);

    progress.setRange(0, 1000000);

    vb.addWidget(&progress);
    vb.addWidget(&btn);
    setLayout(&vb);
    }

    void QTMainForm::OnBtnstrt()
    {
    double i=0;
    while(i<=1000000)
    {
    i++;
    qApp->processEvents();

    progress.setValue(i);
    }
    }

    still the same problem occurs. As I was observing if I put the value of "i" 1000000 the process stop when I close the window but if I put the value of "i" >1000000 then the problem occurs. any suggestions?

  13. #10
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: qt window close but process still running

    Can you post a compilable code...

    1. I am still guessing QTMainForm is QWidget/QMainWindow ?
    2. How and where you are creating QTMainForm ?
    3. Did to follow suggestion by d_stranz above
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  14. The following user says thank you to Santosh Reddy for this useful post:

    sazzad08 (2nd January 2013)

  15. #11
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    5

    Default Re: qt window close but process still running

    yes. you are right. this is my full code

    class QTMainForm : public QWidget
    {
    Q_OBJECT

    public:
    QTMainForm(QWidget* parent=0);
    ~QTMainForm();



    private slots:

    void OnBtnstrt();
    // void closer();
    // DWORD WINAPI SocketHandler(void*);

    private:

    QProgressBar progress;
    QPushButton btn;

    //QVBoxLayout vboxMainLayout;
    QVBoxLayout vb;

    };


    QTMainForm::QTMainForm(QWidget* parent)
    {

    btn.setText("Start");
    connect(&btn, SIGNAL(clicked()), this, SLOT(OnBtnstrt()));
    progress.setValue(0);

    progress.setRange(0, 10000000);

    vb.addWidget(&progress);
    vb.addWidget(&btn);
    setLayout(&vb);
    }

    void QTMainForm::OnBtnstrt()
    {
    double i=0;
    while(i<=10000000)
    {
    i++;
    qApp->processEvents();

    progress.setValue(i);

    }
    }

    QTMainForm::~QTMainForm()
    {

    }


    int main(int argc, char *argv[])
    {

    QTMainForm* pMainForm = 0;

    QApplication app(argc, argv);
    pMainForm = new QTMainForm();
    pMainForm->resize(300, 300);
    pMainForm->show();
    return app.exec();
    }

  16. #12
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: qt window close but process still running

    Not sure what is your long term goal, whatever it be using such a for loop(> 10000000) in button click (or any other slot) will land in to various issues.\

    For now to solve the problem, just modify the button click slot
    Qt Code:
    1. void QTMainForm::OnBtnstrt()
    2. {
    3. double i=0;
    4. while(i<=10000000)
    5. {
    6. i++;
    7. qApp->processEvents();
    8.  
    9. progress.setValue(i);
    10.  
    11. if(!isVisible()) // <<<<<<<<<<<<<<<<<<<<<
    12. break;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    One more observation in the main, pMainForm is not being deleted, this is a leak in memory (it will not be a concern now, as the app exits anyway). As a good practice (in Qt) always pass the parent handle to the base classs QWidget/QObject

    Qt Code:
    1. QTMainForm::QTMainForm(QWidget* parent)
    2. : QWidget(parent) // <<<<<<<<<<<<<<<<<<<<<<<<<<<
    3. {
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  17. The following user says thank you to Santosh Reddy for this useful post:

    sazzad08 (2nd January 2013)

  18. #13
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    5

    Default Re: qt window close but process still running

    it works great. thanks a lot.

Similar Threads

  1. Replies: 4
    Last Post: 27th June 2012, 10:41
  2. Replies: 1
    Last Post: 10th April 2012, 16:18
  3. Get All Running Process Win32
    By METEOR7 in forum Qt Programming
    Replies: 7
    Last Post: 4th December 2011, 13:05
  4. Get Process ID for a running application
    By bob2oneil in forum Qt Programming
    Replies: 5
    Last Post: 10th September 2011, 21:58
  5. Destroyed while process is still running
    By qtzcute in forum Qt Programming
    Replies: 5
    Last Post: 23rd July 2009, 08:26

Tags for this Thread

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.