题
我正在尝试运行在其他线程中复制文件的代码,以免它冻结应用程序的GUI。
我发现它似乎无法在单独的线程中工作。
为什么不起作用?
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;
}
解决方案
由于您没有发布代码,因此我可以尝试猜测问题是什么。 QT具有静态功能:
bool copy ( const QString & fileName, const QString & newName )
还有一个不静态的副本:
bool copy ( const QString & newName )
如果文件定义为 newName
已经存在了,即。现有文件不会被覆盖。另外,也许路径不存在。如果没有一部分代码,就很难猜测问题是什么。
不隶属于 StackOverflow