Search code examples
c++qt6

Qt5 QtConcurrent rewrtie for Qt6


In Qt5 I have used the QtConcurrent like

void  MainWindow::startServerBG(int arg){
}
QFuture<void> future = QtConcurrent::run(this,&MainWindow::startServerBG,arg);

But the same code gives compilation error in Qt6.

The doc specify some changes in function, but I cannot understand, can anyone tell how I rewrite above code for Qt6

Error

mainwindow.cpp:96: error: static assertion failed: The first argument of passed callable object isn't a QPromise & type. Did you intend to pass a callable which takes a QPromise & type as a first argument? Otherwise it's not possible to invoke the function with passed arguments.


Solution

  • The signature of QConcurrent::run has been changed to work with a variable number of arguments:

    template <typename T>
    QFuture<T> run(Function &&f, Args &&...args)
    

    So, to fix your code simply change

    QFuture<void> future = QtConcurrent::run(this,&MainWindow::startServerBG,arg);
    

    to

    QFuture<void> future = QtConcurrent::run(&MainWindow::startServerBG, this, arg);
    

    https://doc.qt.io/qt-6/concurrent-changes-qt6.html#qtconcurrent-run