[Interest] parallel asynchrone action

Sylvain Pointeau sylvain.pointeau at gmail.com
Sat Mar 2 21:11:50 CET 2019


Dear all,

I made an action processed in another thread in QML, and I would like your
review.

When in QML, I click on a button, I send a signal to an object
"TaskProcessor" (declared in QML) to start the action.
Within this slot I start a future with QtConcurrent.

Before I was using QFutureWatcher to know when the future is finished, but
I observed a delay of 1 seconde if the future was taking 4 secondes to
complete.
so now I use one object ("TaskNotifier") instantiated in the concurrent
lambda, that connect to "this", and trigger a message to "TaskProcessor"
when it finishes.
TaskProcessor then triggers a signal to QML with the result.

Is this approach good? I was wondering if this is really the way to follow
Do you use another approach?

in QML:

Button {

    id: btnStart

    text: "Start"


    onClicked: {

        output.text = "computing..."

        taskc.doCalculation(Number(p1.text),Number(p2.text))

        enabled = false

    }

}


TaskComputer {

    id: taskc


    onCalculationFinished: {

        btnStart.enabled = true


        *// do something with rs*

        }


}


in C++:

void TaskComputer::doCalculation(int p1, int p2)

{

    QFuture<void> f1 = QtConcurrent::run( [this, p1, p2]() -> int {


        TaskNotifier tnotifier;

        connect(&tnotifier, &TaskNotifier::finished, this,
&TaskComputer::futureFinished);


        // do the task


        TaskResult tres(/*...my results...*/);


        emit tnotifier.finished(tres);

    } );

}


void TaskComputer::futureFinished(TaskResult rs)

{

    // do some stuff


    emit calculationFinished(rs);

}


Best regards,
Sylvain
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190302/9c4a3dad/attachment.html>


More information about the Interest mailing list