Peter Fašianok
Peter Fašianok

Reputation: 153

Calling method in other thread QT/C++

Is possible call method in background thread in same class? Using C++/QT without c++11. Or repeatedly everly 5 seconds run foo2.

Example

class MyClass
{
  public:
     void foo(...)
     {
        // in another thread run foo2
        foo2;
     }
  .
  .
  .
  protected:
     void foo2(...){}

}

thanks

Upvotes: 1

Views: 1376

Answers (1)

Shf
Shf

Reputation: 3493

to run some function in a separate thread you can use QtConcurrent::run (i use it with QFutureWatcher). To run it every 5 or so seconds, use QElapsedTimer class

QFuture<void> future = QtConcurrent::run(this, &MyClass::foo2, ...foo2 arguments);

http://qt-project.org/doc/qt-4.8/qtconcurrentrun.html#run or check it here https://stackoverflow.com/search?q=QtConcurrent%3A%3Arun

or you can subclass QThread, reimplement run() with the stuff you want to happen in your thread, and then create an instance of your thread and call start() on it.

Upvotes: 4

Related Questions