我想用另一个文件替换我的驱动器上的文件。经过研究后,我发现没有“替换”方法。相反,我将不得不删除我的原始文件,然后将我的临时文件移动到其位置。当我这样做时,我没有看到我更新的临时文件被移动到其新目录中。
QTemporaryFile tempFile;
if (tempFile.open())
{
// Create temp file
QTextStream stream;
stream.setDevice(&tempFile);
document.save(stream, 4); // document is a QDomDocument object
tempFile.close();
// Replace original file with temp file
file.remove();
tempFile.rename("C:\\ProgramData\\Foo\\data.xml"); // I don't see the updated data.xml
}
答案 0 :(得分:1)
您的问题似乎来自使用TypeError: Cannot read property 'pageName' of undefined.
。
首先,请注意QTemporaryFile
什么都不做。
从Qt代码:
tempFile.close();
其次,当你致电bool QTemporaryFileEngine::close()
{
// Don't close the file, just seek to the front.
seek(0);
setError(QFile::UnspecifiedError, QString());
return true;
}
时,
它将有效地关闭tempFile.rename("C:\\ProgramData\\Foo\\data.xml");
,但它也会更改tempFile
,这意味着当tempFile.fileName()
对象被删除时,它将删除您复制到的文件。如果您想阻止这种情况,可以致电tempFile
您尝试做的另一个解决方案是使用QSaveFile为您处理“保存到临时/关闭/替换”例程。