1 year ago

#112664

test-img

Fede

How do I delete a file in Qt when the file is already in the file system?

I have a problem when I try to delete a file with Qt. If I delete it with my Qt application in the same run as the file is created there is no issues, if I try to delete it when it already exists before the application has been launched, I receive an "access denied" issue. Looking to this code:

int main(int argc, char *argv[])
{
    QString path = "MyPath"; 

    QFile file(path+"/test.txt");

    if (file.open(QFile::ReadWrite)){
        if(!file.setPermissions(QFileDevice::WriteUser | QFileDevice::ReadUser|
                    QFileDevice::ExeUser)){
            std::cout << "Error in permissions" << std::endl;
         }
        file.close();
        if(!QDir().remove(path+"/test.txt"))
            std::cout << "Can't delete" << std::endl;
    }
}

If I lanch the application, the file is created and then delete without any problem. If I try to access the file in Qt when it has already been created (by the application in a previous run, skipping the remove part of code) the file can't be delete or accessed. I have tried to set the permissions but nothing has changed

c++

qt

qfile

0 Answers

Your Answer

Accepted video resources