添加进度条时出错

Error while adding progess bar

本文关键字:出错 添加      更新时间:2023-10-16

我试图在qt中添加进度条,但出现以下错误:

error: no matching function for call to 'map(QVector<int>&, <unresolved overloaded function type>)'
      futureWatcher.setFuture(QtConcurrent::map(vector, spin));

代码如下:

using namespace QtConcurrent;
const int iterations = 20;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QVector<int> vector;
    for (int i = 0; i < iterations; ++i)
        vector.append(i);
    QProgressDialog dialog;
    dialog.setLabelText(QString("Progressing using %1 thread(s)...").arg(QThread::idealThreadCount()));
    QFutureWatcher<void> futureWatcher;
    QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int,int)), &dialog, SLOT(setRange(int,int)));
     QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), &dialog, SLOT(setValue(int)));
     futureWatcher.setFuture(QtConcurrent::map(vector, spin));
  dialog.exec();
}
void MainWindow::spin(int &iteration)
{
    const int work =1000 * 1000 * 40;
    volatile int v =0;
    for(int j = 0; j< work; j++)
        ++v;
    qDebug()<< "iteration" <<iteration <<"in thread"<<QThread::currentThreadId();
}

我使用的是Qt5.2.1。无法重新组织错误。请帮我解决这个错误。

在行中

futureWatcher.setFuture(QtConcurrent::map(vector, spin));

您应该指定spin函数的类名,并使用例如std::bindspin与类对象绑定。或者您也可以使用全局函数。