我需要将文件写入我正在设计的应用程序的Application Data目录中。我使用Qt在Ubuntu下开发。以下代码段显示了我的写函数:
FileOperations::Error FileOperations::saveUsage(SensorIdentification sensorID, QList<UsageListModel::Usage> &usageList)
{
QString fileName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
fileName.append("/");
fileName.append("testnaam");
fileName.append(FILE_EXTENSION);
this->sensorID = sensorID;
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly))
{
return Error::FileNotOpen;
}
QDataStream out(&file);
out << sensorID;
for(int i = 0; i < usageList.size(); i++)
out << usageList.at(i).date << usageList.at(i).totalSteps << usageList.at(i).wearTime;
if(file.size())
{
file.close();
return Error::NoError;
}
else
{
file.close();
return Error::WritingToFileFailed;
}
}
追加后的fileName是:"/home/jan/.local/share/Foo/Bar With Spaces/testnaam.liv"
出于某种原因,我无法在该目录中写入文件。当我写入用户文档文件夹时,它可以正常工作。为什么我不能写到appdata文件夹?