Qt 多线程QtConcurrent::run使用进度条

	QProgressDialog process(this);
	process.setWindowTitle(u8"测试");
	process.setLabelText(QString(u8"读取%1张影像...").arg(imagePaths.size()));
	process.setRange(0, imagePaths.size());
	process.setValue(0);
	process.setModal(true);
	process.setCancelButton(nullptr);//隐藏取消按钮
	process.show();

	//进程监视
	QFutureWatcher<void> *watcher = new QFutureWatcher<void>;
	QEventLoop loop;
	connect(watcher, &QFutureWatcher<void>::finished, &loop, &QEventLoop::quit);
	connect(watcher, &QFutureWatcher<void>::progressValueChanged, &process, &QProgressDialog::setValue);

	QFuture<void> future = QtConcurrent::run([&]()
	{
		qDebug() << "线程开始" << QThread::currentThread();
		for (int i = 0; i < imagePaths.size(); ++i)
		{
			emit watcher->progressValueChanged(i + 1);
		}
		qDebug() << "线程结束" << QThread::currentThread();
	});
	watcher->setFuture(future);
	loop.exec();

亲测可用

你可能感兴趣的:(qt,c++,多线程)