Results 1 to 13 of 13

Thread: qt window close but process still running

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 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.

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

    sazzad08 (2nd January 2013)

  4. #3
    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();
    }

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 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.

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

    sazzad08 (2nd January 2013)

  7. #5
    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
  •  
Qt is a trademark of The Qt Company.