QtConcurrent给出了错误

时间:2016-04-21 15:27:33

标签: c++ qt

鉴于以下代码,我收到以下错误:

  

fileio.cpp:47:错误:C3867:' FileIO :: read&#39 ;:非标准语法;使用   '&安培;'创建指向成员的指针

     

fileio.cpp:47:错误:C2780:' QFuture   QtConcurrent :: map(Iterator,Iterator,MapFunctor)':需要3个参数    - 提供2个

为什么?

fileio.h

#ifndef FILEIO_H
#define FILEIO_H

#include <QtCore>

class FileIO : public QObject
{
    Q_OBJECT
    Q_DISABLE_COPY(FileIO)

signals:
    void finished();

public slots:
     Q_INVOKABLE void cancel();

private:
    void read(QString &fileURL);

public:
    FileIO(QObject *parent = 0);
    ~FileIO();


    Q_INVOKABLE void read_b64(const QString &fileURL);

};

#endif // FILEIO_H

fileio.cpp

#include "fileio.h"
#include <QFile>
#include <QDebug>
#include <QUrl>
#include <QThread>
#include <QtConcurrent>

FileIO::FileIO(QObject *parent)
    : QObject(parent)
{
}

FileIO::~FileIO()
{
}

void FileIO::read(QString &filePath)
{
    QByteArray finalBytes = QByteArray();

    filePath = QUrl(filePath).toLocalFile();

    QFile file(filePath);

    qDebug() << "file size: " << file.size();
    if (file.open(QIODevice::ReadOnly)) {
        finalBytes = file.readAll();
    }

    qDebug() << "byte size: " << finalBytes.length();

    //bytes.toBase64();
}


void FileIO::read_b64(const QString &filePath)
{

    QFutureWatcher<void> futureWatcher;
    QObject::connect(this, &FileIO::cancel, &futureWatcher, &QFutureWatcher<void>::cancel, Qt::UniqueConnection);
    QObject::connect(&futureWatcher, &QFutureWatcher<void>::finished, this, &FileIO::finished);


    QStringList filePaths;
    filePaths << filePath;

    QFuture<void> future = QtConcurrent::map(filePaths, this->read);

    futureWatcher.setFuture(future);
}

1 个答案:

答案 0 :(得分:0)

this->read无效。您希望使用std::bind将非静态成员函数绑定到一个实例创建一个callable,然后调用QtConcurrent::map的第一个重载,带两个参数:

QFuture<void> future =
    QtConcurrent::map(filePaths, std::bind(&FileIO::read, this));