现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

利用Qt的QFutureSynchronizer机制同步拷贝文件

2015-04-19 20:29 工业·编程 ⁄ 共 1390字 ⁄ 字号 暂无评论

QMap< QString, QString > m_Files;
QMap< QString, QString > m_FileMimetypes;
QHash<QString, QString> ImportEPUB::LoadFolderStructure()
{
    QList<QString> keys = m_Files.keys();
    int num_files = keys.count();
    QFutureSynchronizer<tuple<QString, QString> > sync;

    for (int i = 0; i < num_files; ++i) {
        QString id = keys.at(i);
        sync.addFuture(QtConcurrent::run(
                           this,
                           &ImportEPUB::LoadOneFile,
                           m_Files.value(id),
                           m_FileMimetypes.value(id)));
    }

    sync.waitForFinished();
    QList<QFuture<tuple<QString, QString> > > futures = sync.futures();
    int num_futures = futures.count();
    QHash<QString, QString> updates;

    for (int i = 0; i < num_futures; ++i) {
        tuple< QString, QString > result = futures.at(i).result();
        updates[result.get<0>()] = result.get< 1 >();
    }

    updates.remove(UPDATE_ERROR_STRING);
    return updates;
}

tuple<QString, QString> ImportEPUB::LoadOneFile(const QString &path, const QString &mimetype)
{
    QString fullfilepath = QFileInfo(m_OPFFilePath).absolutePath() + "/" + path;
   
    try {
        Resource &resource = m_Book->GetFolderKeeper().AddContentFileToFolder(fullfilepath, false, mimetype);
        QString newpath = "../" + resource.GetRelativePathToOEBPS();
        return make_tuple(fullfilepath, newpath);
    } catch (FileDoesNotExist &) {
        qDebug() << "FNDE: " << path;
        return make_tuple(UPDATE_ERROR_STRING, UPDATE_ERROR_STRING);
    }
}

给我留言

留言无头像?