Solved Why can't QSettings be written to QTemporaryFile
-
@JonB I don't see why it's needed - all what is needs to be done is written here. Create a QTemporaryFile, open it, close it again, pass filename to QSettings and go ahead.
-
@Christian-Ehrlicher said in Why can't QSettings be written to QTemporaryFile:
Create a QTemporaryFile, open it, close it again, pass filename to QSettings and go ahead.
I believe the OP here tried that earlier (his post before last above) but got the same "access error", did he not? See his earlier code above:
QTemporaryFile tmpFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath(), QApplication::applicationName()), this); tmpFile.open(); tmpFile.close();
And probably that reference tried to.
QTemporaryFile::close()
does not close the file, reference the docs I quoted earlier, and I looked at the source and all it does itseek(0)
. Doubtless to do with keeping a handle into it until theQTemporaryFile
goes out of scope, whereupon it will be closed and deleted. Hence if the fact that it is kept open (for all I know with exclusive access) is what stopsQSettings
working cannot be avoided. -
@JonB You're right. It can't work since completely closing a QTemporaryFile will delete it (as this is the usecase for this class).
I also don't see a reason to use a QTemporaryFile here at all. -
@tovax might have better luck using QTemporaryDir, and then creating a QFile with a known name within.
-
Here is what the docs say:
Reopening a QTemporaryFile after calling close() is safe. For as long as the QTemporaryFile object itself is not destroyed, the unique temporary file will exist and be kept open internally by QTemporaryFile.
So, the temporary file is kept open which is why QSettings cannot write to it. In the end, you just need a temporary file name. How about this:
QString tmpFileName; { QTemporaryFile tmpFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath(), QApplication::applicationName()), this); tmpFile.open(); tmpFileName = tmpFile.fileName(); } ...
Note the curly braces to restrict the scope if of the temporary file (so it will actually be closed). I cringed a little when I saw that you leaked a pointer to the temporary file because you used
new
, but neverdelete
d it. -
@SimonSchroeder said in Why can't QSettings be written to QTemporaryFile:
So, the temporary file is kept open which is why QSettings cannot write to it. In the end, you just need a temporary file name. How about this:
That is what I quoted/wrote earlier on.
I do not understand why you think your code will help the OP. It is true that while the
QTemporaryFile tmpFile
is in scope Qt holds an open handle into it, which seems to stopQSettings
writing to it. However I have previously quoted from the docs, maybe you did not notice, https://doc.qt.io/qt-5/qtemporaryfile.html#detailsand the file will subsequently be removed upon destruction of the QTemporaryFile object.
The whole point is that when it goes out of scope to release the handle it also deletes the file. OP cannot write to file within the scope and the file will not exist any longer out of scope.
@tovax
As I sad earlier. Do not useQTemporaryFile
here. Use static QString QDir::tempPath() to make your own temporary file and use that instead. It's not hard to make sure you delete that file explicitly when you are done with it. -
@SimonSchroeder Hi, thank you very much for your reply. I think the tmpFile will be deleted out of the curly braces.
-
@Christian-Ehrlicher @jeremy_k @JonB @Pl45m4 @SimonSchroeder Thank you very much! QTemporaryDir works.
Output:
1 "temp dir:" "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug/debug/.hsxxlg"
2 "temp file:" "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug/debug/.hsxxlg/JCDemoAES.tmp"
3 "settings status:" QSettings::NoError
4 "temp file content:" [GroupTest]
UserCode=0000, 1111, 2222, 3333
UserCodeDefault=1234// Temp dir QTemporaryDir tmpDir(QStringLiteral("%1/").arg(QApplication::applicationDirPath())); qDebug() << 1 << QString("temp dir:") << tmpDir.path(); // Temp file QFile tmpFile(QStringLiteral("%1/%2.tmp").arg(tmpDir.path(), QApplication::applicationName()), this); tmpFile.open(QFile::ReadWrite | QFile::Text | QFile::Truncate); tmpFile.close(); qDebug() << 2 << QString("temp file:") << tmpFile.fileName(); // Settings QSettings settings(tmpFile.fileName(), QSettings::IniFormat); settings.setIniCodec(QTextCodec::codecForName("UTF-8")); settings.beginGroup("GroupTest"); QStringList list; list << QStringLiteral("0000") << QStringLiteral("1111") << QStringLiteral("2222") << QStringLiteral("3333"); settings.setValue("UserCode", list); settings.setValue("UserCodeDefault", QStringLiteral("1234")); settings.endGroup(); settings.sync(); // very important qDebug() << 3 << QString("settings status:") << settings.status(); // Read temp file tmpFile.open(QFile::ReadOnly | QFile::Text); QByteArray ba = tmpFile.readAll(); qDebug() << 4 << QString("temp file content:") << ba.data(); tmpFile.close();
-
Reopening a QTemporaryFile after calling close() is safe. For as long as the QTemporaryFile object itself is not destroyed, the unique temporary file will exist and be kept open internally by QTemporaryFile.
-
@tovax said in Why can't QSettings be written to QTemporaryFile:
I think the tmpFile will be deleted out of the curly braces.
That is correct. But, that doesn't matter because if it is gone, it is not opened by someone else anymore. There is nothing stopping you from reusing that exact file name. There is also a member function in
QTemporaryFile
to keep the file after your temporary file object is gone.