문제

I am trying to run code of copying files in other thread so that it may not freeze the GUI of the application.

I found that it does not seem to work in a separate thread.

Why is it not working ?

void CopyOperation::run()
{ 
    CopyFilesToFolder(list,sFolder); 
}

bool CopyOperation::CopyFilesToFolder(const QStringList &oFileList,const QString 
&sTargetFolder)
{

if(sTargetFolder.isEmpty())
 {

    status = false;
    return false;
}

QDir dir(sTargetFolder);

if(!dir.exists()) dir.mkdir(sTargetFolder);

QString sOldDirPath = dir.currentPath();

//if(!dir.setCurrent(sTargetFolder)) return false;

QFile file;
status = true;

foreach(QString sFileName,oFileList)
{
    file.setFileName(sFileName);

    QFileInfo info(sFileName);

    QString newfile =  sTargetFolder + "/" +  info.fileName();

    qDebug() << "\n name = " << newfile;

    if(!QFile::copy(sFileName,newfile))
    //if(!file.copy(newfile))
    {
        qDebug() << "\n File copy failed .. " + file.fileName() + " Error : " + file.errorString();
        status = false;
        break;
    }

}

qDebug() << "\n Result .. " << file.errorString() << "code " <<  file.error();
//dir.setCurrent(sOldDirPath);

return status;
}
도움이 되었습니까?

해결책

Since you didn't post code, I just can try to guess what is the problem. Qt has a static function:

bool copy ( const QString & fileName, const QString & newName )

There is also a copy which is not static:

bool    copy ( const QString & newName )

Both of them will fail if file defined by newName already exists, ie. existing file will not be overwritten. Also, maybe path doesn't exists. Without some portion of code is difficult to guess what is the problem.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top