Important: Please read the Qt Code of Conduct -

How to use QtConcurrent::run

  • I have a class:

    @class MyThread : public QThread
    MyThread (QObject *parent = 0);

    void nextCalculationReady(const double value) const;

    void run();

    void asyncFunction(const double x) const;

    MyThread::MyThread(QObject *parent) :
    { }

    void MyThread::run()
    double x = 0.0;
    //do something
    QtConcurrent::run(this, &MyThread::asyncFunction, x); // QFuture object is not needed. It doesn't work
    // QtConcurrent::run(this, &MyThread::asyncFunction, x); // It doesn't work
    // QtConcurrent::run((MyThread
    )this, &MyThread::asyncFunction, x); // It doesn't work

    void MyThread::asyncFunction(const double x) const
    //do something
    emit nextCalculationReady(x);

    I want run asyncFunction asynchronously in another thread. But when I write *this or simply this in a QtConcurrent::run - function call I get compiler issue.

    I've read the documentation and maybe I've missed something.
    How to use QtConcurrent::run with calling a member function of class?

  • If I use @QtConcurrent::run(*this, &MyThread::asyncFunction, x);@

    Compiler error:

    c:\QtSDK\Desktop\Qt\4.8.0\msvc2010\include\QtCore\qthread.h:137: error: C2248: 'QObject::QObject' : cannot access private member declared in class 'QObject'

    If I use @QtConcurrent::run(this, &MyThread::asyncFunction, x);@

    Compiler error:
    c:\qtsdk\desktop\qt\4.8.0\msvc2010\include\qtcore\qtconcurrentrun.h:123: error: C2064: term does not evaluate to a function taking 2 arguments

Log in to reply