2016-04-21 148 views
0

考慮下面的代碼,我得到以下錯誤:QtConcurrent給出錯誤

fileio.cpp:47: error: C3867: 'FileIO::read': non-standard syntax; use '&' to create a pointer to member

fileio.cpp:47: error: C2780: 'QFuture QtConcurrent::map(Iterator,Iterator,MapFunctor)': expects 3 arguments - 2 provided

這是爲什麼?

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); 
} 

回答

0

this->read無效。你想用std::bind綁定與實例的非靜態成員函數來創建一個調用,然後調用QtConcurrent::map第一過載,取兩個參數:

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

包括並替換該行,但仍然得到錯誤.. – davidkomer

+0

沒關係簡單地聲明沒有類命名空間的讀取(不確定正確的單詞)工作。即void讀取不是void FileIO ::讀取 – davidkomer

+0

現在它工作:D – davidkomer