文件复制操作不能在单独的线程中进行吗?

发布于 2024-10-10 08:19:33 字数 1167 浏览 0 评论 0原文

我正在尝试在其他线程中运行复制文件的代码,这样它就不会冻结应用程序的 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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-10-17 08:19:33

由于您没有发布代码,我只能尝试猜测问题是什么。 Qt 有一个静态函数:

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

还有一个不是静态的副本:

bool    copy ( const QString & newName )

如果 newName 定义的文件已经存在,则它们都会失败,即。现有文件不会被覆盖。另外,也许路径不存在。如果没有代码的某些部分,很难猜测问题是什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文