QtConcurrent::run stop running when top-level widged closed.



  • Hi everybody,
    I am a newbie on qt. I have a problem when using QtConcurrent::run(),
    the senario is:

    1. I start from creating a QWidiget project which involves a qpushButton.

    2. I have a long-term blocking function, so I use QtConcurrent::run() in a QPushButton::onClicked() slots:
      like this, and its work well without blocking UI.
      @
      void Widget::on_pushbutton1_clicked()
      {
      QFuture<void> future = QtConcurrent::run(&handler, &handler::longTermWork);
      QFutureWatcher<void> futureWatcher;
      QEventLoop loop;
      QObject::connect(&futureWatcher, SIGNAL(finished()), &loop, SLOT(quit()));
      futureWatcher.setFuture(future);
      loop.exec();
      this->appendOutput(QString::fromLocal8Bit("end run"));
      }
      @

    3. I override void Widget::closeEvent(QCloseEvent *event) to emit a signal which will set stop flag to notify my blocking funtion to return.
      like this:

    @
    Widget::closeEvent(QCloseEvent *event) {
    terminate(); // a signal emission
    event->accept();
    }
    @

    1. But when user click x button. the top-level widget is gone. But process still run in background. I have ensured that the signal and coressponding slot had worked.
    if I sleep about 2 seconds between terminate() and event->accept(), everything is ok, widget is gone and process is finished.
    

    I cannot figure out what wrong about my action.

    Can somebody help my please?



  • Hi,
    And welcome to the Qt-project! Hope you stay a long time like all of us do ;-)
    A quick notion about your post, place code between '@'s and it will be shown as QtCreator code and not as plain text. Helps up to read it, because now I only skim it ;-)
    What you have here is a termination of your eventhandler (topwidget) and the close of it. When you issue a signal it will be handled by the eventhandler later on. But when you close the widget the eventhandler will be stopped were it is, not when it is ready. use void QObject::deleteLater() [slot] function for your top widget. This will make sure all events are handled for that widget and when all is done, it will be cleaned up.
    Hope this helps.
    (sorry, don't want to spoil your coding pleasure, so no code example, reading your post you like to do so).



  • Hi, thank you very much for reply :)

    I have tried QObject::deleteLater() and put it in override function: void Widget::closeEvent(QCloseEvent *event)

    @
    void Widget::closeEvent(QCloseEvent *event)
    {
    emit exitProg();
    event->accept();
    this->deleteLater();
    }
    @

    But still don't work.
    Do I misundertand your suggestion?

    I try to make some simple code stub to clear my situations.

    I have 2 classes:
    StuffHandler and Widget(auto-generated by QtCreator)

    The StuffHandler has a blocking function like this.

    @
    void StuffHandler::blockingRun()
    {
    for(int i = 0; ;i++) {
    if (stopFlag) {
    qDebug() << "program exit";
    break;
    }

        QEventLoop eventloop;
        QTimer::singleShot(500, &eventloop, SLOT(quit()));
        eventloop.exec&#40;&#41;;
    
        qDebug() << "i: " << i;
    }
    

    }
    @

    and I invoke this function with QtConcurrent in Widget::on_pushButton_clicked() slots:

    @
    void Widget::on_pushButton_clicked()
    {
    StuffHandler *h = new StuffHandler(this);
    QFuture<void> future = QtConcurrent::run(h, &StuffHandler::blockingRun);
    QFutureWatcher<void> futureWatcher;
    QEventLoop loop;
    QObject::connect(&futureWatcher, SIGNAL(finished()), &loop, SLOT(quit()));
    QObject::connect(&futureWatcher, SIGNAL(finished()), h, SLOT(deleteLater()));
    futureWatcher.setFuture(future);
    loop.exec();
    }
    @

    Then I expect the signal/slots mechanism will work, and it worked as expected. I can see "slot invocation " message in debug console.

    @
    connect(w, SIGNAL(exitProg()), this, SLOT(terminate())); // a statement in StuffHandler constructor

    void StuffHandler::terminate() // slots
    {
    stopFlag = true;
    qDebug() << "slot invocation";
    }
    @

    But still a process executing in background and the top-level wiget is gone.

    Question:

    1. I still cannot understand why a process is executing in background but the invoked thread stop running. (it doesn't print debug mssage every 0.5 seconds.)

    2. Do I make a good practice for using QtConcurrent?

    Thank you for everyone.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.