Weird result with QSettings access error
-
Hello,
I do not understand the result of the
QSettings::status()
function in this case:I have a system and user scope settings file. When I remove the rights to write in the system scope file, the next time I use the user scope settings, I get an
QSettings::AccessError
when I call thestatus
function. However, when I open the user scope file, the value has been written even with the error statusOn the opposite, if I remove the rights to write in the user scope file, I get errors only when trying to write in the user scope settings.
Why the status is always at
AccessError
when the system scope in not writable ?Here is a minimal reproducible code:
#include <QSettings> #include <QDebug> void printStatus(const QSettings& settings) { switch (settings.status()) { case QSettings::NoError: qInfo() << settings.scope() << "No error"; break; case QSettings::AccessError: qInfo() << settings.scope() << "Access error"; break; case QSettings::FormatError: qInfo() << settings.scope() << "Format error"; break; default: break; } } void testSettings(QSettings::Scope scope, int value) { QSettings settings { QSettings::IniFormat, scope, "TestQSettings", "TestQSettings" }; settings.sync(); printStatus(settings); settings.setValue("test", value); settings.sync(); printStatus(settings); } int main(int argc, char *argv[]) { testSettings(QSettings::UserScope, 40); testSettings(QSettings::SystemScope, 40); testSettings(QSettings::UserScope, 42); testSettings(QSettings::SystemScope, 42); }
And this is what I obtain when the system scope file only is not writable:
QSettings::UserScope No error QSettings::UserScope No error QSettings::SystemScope No error QSettings::SystemScope Access error QSettings::UserScope Access error QSettings::UserScope Access error QSettings::SystemScope Access error QSettings::SystemScope Access error
My system settings:
- Windows 10
- MSVC 2019
- Qt 5.15.2
-
I also reproduced this behavior with Qt 6.2.4
This bug seems to match with what I encountered but even by creating a new
QSettings
, the problem remains. -
@Loic-B
Did you at least try callingQSettings::sync()
after the first error, to see whether that clears it?