QFile“remove()”不删除在执行时众所周知的文件

问题描述 投票:0回答:1

我有一个由QProcess *调用的进程。创建之后,我将其finished()信号连接到一个删除一些结果文件的插槽。此过程创建一组在执行时众所周知的文件。我正在为每个人创建一个QFile *对象。我试图在指定的进程完成后删除这些文件。

问题是:如果我尝试用QFile :: remove()或QDir :: remove()删除它们,似乎什么也没发生。但是,如果我尝试重命名它们,它会重命名,但在进程完成之前 - 即使已经完成了已完成的信号。此外,QDir :: removeRecursively()删除它们。我试图创建一个新目录,将文件移动到该文件夹​​并使用Qdir :: removeRecursively() - 甚至删除了我的所有桌面 - 但是,正如我所说,在进程具有之前调用了“删除文件”插槽真的完蛋了我试图使用QProcess :: waitForFinished()来防止这种过早的调用,但它没有用。

顺便说一句,我问用户在哪里保存文件,这样当我尝试删除文件时,它们可以保存在任何地方。另外,我不能要求管理员权限。

谢谢!

QProcess调用

QProcess *execute_call = new QProcess(this);
execute_call->setWorkingDirectory(lastSavingLocation + "/control/");
execute_call->setProgram(execute_call->workingDirectory() + "execute.bat");
connect(execute_call, SIGNAL(finished(int)), this, SLOT(DeleteExecutionFiles(int)));
execute_call->start();
execute_call->waitForFinished(-1);
//I tried to put this 
//before the start() call, but nothing seems to change

DeleteFiles Slot

void MainWindow::DeleteExecutionFiles(int status)
{
    if(status == 0)
    {
        qDebug() << "slot called";
        QFile *lof_mh = new QFile(lastSavingLocation + "/lof-mh.exe");
        QFile *libgomp = new QFile(lastSavingLocation + "/libgomp-1.dll");
        QFile *libwin = new QFile(lastSavingLocation + "/libwinpthread-1.dll");
        QFile *tests_env = new QFile(lastSavingLocation + "/control/lof-mh-testenvironment-tool.exe");
        QFile *execute = new QFile(lastSavingLocation + "/control/execute.bat");
        QFile *execute_bat = new QFile(lastSavingLocation + "/control/executeParallel.bat");

        lof_mh->setFileName(lastSavingLocation + "/lof-mh.exe");
        libgomp->setFileName(lastSavingLocation + "/libgomp-1.dll");
        libwin->setFileName(lastSavingLocation + "/libwinpthread-1.dll");
        tests_env->setFileName(lastSavingLocation + "/control/lof-mh-testenvironment-tool.exe");
        execute->setFileName(lastSavingLocation + "/control/execute.bat");
        execute_bat->setFileName(lastSavingLocation + "/control/executeParallel.bat");

        lof_mh->remove();
        libgomp->remove();
        libwin->remove();
        tests_env->remove();
        execute->remove();
        execute_bat->remove();
    }
}

编辑1

使用qDebug()和QFile :: errorString()之后得到了这个

lof_mh.remove();                      
qDebug() << lof_mh.errorString(); //"Negated Access"  
libgomp.remove();                     
qDebug() << libgomp.errorString(); //"Negated Access"  
libwin.remove();                      
qDebug() << libwin.errorString(); //"Negated Access"     
tests_env.remove();                   
qDebug() << tests_env.errorString(); //"Negated Access"  
execute.remove();                     
qDebug() << execute.errorString(); //"Unknown Error"    
execute_bat.remove();                 
qDebug() << execute_bat.errorString(); //"Unknown Error"
c++ qt qt5 qprocess qfile
1个回答
1
投票

我设法删除了更改文件权限的文件:

lof_mh.setPermissions(QFileDevice::WriteUser | QFileDevice::ReadUser | QFileDevice::ExeUser);
libgomp.setPermissions(QFileDevice::WriteUser | QFileDevice::ReadUser | QFileDevice::ExeUser);
libwin.setPermissions(QFileDevice::WriteUser | QFileDevice::ReadUser | QFileDevice::ExeUser);
tests_env.setPermissions(QFileDevice::WriteUser | QFileDevice::ReadUser | QFileDevice::ExeUser);
execute.setPermissions(QFileDevice::WriteUser | QFileDevice::ReadUser | QFileDevice::ExeUser);
execute_bat.setPermissions(QFileDevice::WriteUser | QFileDevice::ReadUser | QFileDevice::ExeUser);

之后,我正常地使用QFile :: remove()删除它们。这带来了另一个问题:当QProcess *的finished()信号被激活时,应该调用DeleteFiles()槽,但是当proccess仍在运行时调用slot,因此文件被删除并且它丢失了轨道的文件和崩溃。无论如何,我会创建另一篇文章来检查特定于此主题的答案。

感谢所有帮助过的人!

© www.soinside.com 2019 - 2024. All rights reserved.