复制功能有问题

时间:2011-05-03 03:25:58

标签: qt copy

我在文件浏览器上工作,并且我有一个允许复制文件夹和文件的功能。我面临的问题是我的函数似乎适用于单个文件夹复制操作和多个文件操作。但是当我尝试用文件复制多个文件夹时,里面有子文件夹的foder似乎没有被复制。

我在这里做错了什么。请提出建议。这是我写的代码

void Browser::copy()
{
    MarkClicked=false;

    if(multiSelect == true)
    {
        for(int i=0;i<indexList.size();i++)
        {

            qDebug()<<"indexlist.at(i)= "<<indexList.at(i);
            source_files.append(model->filePath(indexList.at(i)));

        }
    }
    else
    {
        index=list->selectionModel()->currentIndex();
        source_file = (model->filePath(index));
    }
    CopyClicked=true;
    copyAct->setEnabled(false);
    CopyMenuRemoved=true;
    pasteAct->setEnabled(true);
    PasteMenuRmoved=false;

}


/*
    This will perform paste operation.
*/
void Browser::pasteFile()
{

    CopyClicked=false;

    dest_index=list->selectionModel()->currentIndex();
    QString destinationDir= model->filePath(dest_index);

    if(multiSelect == true)
    {

        progress = new QProgressDialog("Copying...","",0, indexList.size(),this,Qt::SplashScreen);
        progress->setAttribute(Qt::WA_DeleteOnClose);
        progress->setWindowModality(Qt::WindowModal);

        for(int i=0;i<indexList.size();i++)
        {
            progress->setValue(i);
            progress->show();

            if(model->isDir(indexList.at(i)))
            {
                QDir srcdir(source_files.at(i));
                destinationDir = model->filePath(dest_index)+"/"+srcdir.dirName();
            }
            else
            {
                destinationDir = model->filePath(dest_index);
            }
                fileInfo=QFileInfo(source_files.at(i));
                copySingle(source_files.at(i),destinationDir);
        }
        progress->close();
        unmarkAll();
    }
    else
    {
        progress = new QProgressDialog("Copying...","",0, 0,this,Qt::SplashScreen);
        progress->setAttribute(Qt::WA_DeleteOnClose);
        progress->setWindowModality(Qt::WindowModal);
        progress->show();
        if(model->isDir(index))
        {
            QDir srcdir(source_file);
            destinationDir = model->filePath(dest_index)+"/"+srcdir.dirName();
            qDebug()<<"destination dir ===="<<destinationDir;
        }else
        {
            destinationDir = model->filePath(dest_index);
            qDebug()<<"destination dir ===="<<destinationDir;
        }
        fileInfo=QFileInfo(source_file);
        copySingle(source_file,destinationDir);
        unmarkAll();
    }
        progress->close();
        unmarkAll();


    copyAct->setEnabled(true);
    CopyMenuRemoved=false;
    pasteAct->setEnabled(false);
    PasteMenuRmoved=true;

}


void Browser::copySingle(QString source_file,QString destinationDir)
{
    if(model->fileInfo(index).isDir())
    {
          sourceDir=QDir(source_file);
          if(!sourceDir.exists())
              return;
          QDir destDir(destinationDir);
          if(!destDir.exists())
          {
              destDir.mkdir(destinationDir);
          }
          QStringList files = sourceDir.entryList(QDir::Files);
          for(int i = 0; i< files.count(); i++)
          {
              QString srcName = source_file + "/" + files[i];
              QString destName = destinationDir + "/" + files[i];
              QFile::copy(srcName, destName);
          }
          files.clear();
          files = sourceDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
          for(int i = 0; i< files.count(); i++)
          {
              QString srcName = source_file + "/" + files[i];
              QString destName = destinationDir + "/" + files[i];
              copySingle(srcName, destName);
          }
    }
    else
    {
          QString destinationFile = destinationDir + "/" + fileInfo.fileName();
          QFile::copy(source_file, destinationFile);

    }
    progress->close();
}

1 个答案:

答案 0 :(得分:0)

对不起,这来得太晚了,也许这就是你要找的东西?

Copy directory using Qt