如何在不创建单独的子类QObject的情况下在单独的线程中调用具有更新进度条能力的函数?
有一个很长的函数可以传递回调函数和数据:
typedef bool (loadProgressFunc) (int percent, void* userHandle);
bool veryLongFunc(int inputData, loadProgressFunc progress, void* userHandle);
如何在单独的线程中调用这个函数,但是让窗口中的进度条被更新?
static bool process(int percent, void* userHandle) {
qDebug() << __PRETTY_FUNCTION__ << percent;
MainWindow* _this = static_cast<MainWindow*>(userHandle);
_this->testFunc(percent);
return true;
}
void MainWindow::testFunc(int percent) {
ui->progressBar->setValue(percent);
}
void MainWindow::startLongFunc() {
veryLongFunc(1, process, this);
}
能。您可以使用QtConcurrent模块和QtConcurrent::run函数。
为了使模块可用,它必须包含在配置文件中: