简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM