[Interest] Issue using QSettings INI format on Windows

Henry Skoglund fromqt at tungware.se
Sat Feb 9 00:16:19 CET 2019

On 2019-02-08 23:56, Murphy, Sean wrote:
>> Hi, just tested your example program in MSVC 2017 Qt 5.12.1 and it fails
>> as well.
>> But a simple fix is just to make sure that the QFile file isn't open at
>> the same time as you write out the settings to it.
>> I.e. in your example program, it suffices to move that first
>> file.close(); up to just before the "// write out via QSettings" comment
>> line is, then it runs flawlessly.
>> But perhaps that's not possible to keep those writes separate in time in
>> your "real" app that your porting?
> Thanks for testing!
> Yes, I discovered that issue as well, and then forgot to post a follow-up
> here. Where I had trouble, and something I did differently in the real
> app vs. the minimal example, is that the file I was trying to use to write
> the settings to was a QTemporaryFile. I stumbled across the same fix you
> did in the example, but that fix never seemed to work in my real app with a
> QTemporaryFile. After testing, I believe that is because the QSettings
> object is trying to open the associated settings file with write permissions,
> and it isn't able to acquire them. In my example, it was because I already
> had opened the file with write permissions (mistakenly thinking I needed
> to do that before all the QSettings stuff). In my real app, I think that it
> must be because QTemporaryFile holds a lock on the file so it can
> remove it when the QTemporaryFile object goes out of scope, and
> therefore the QSettings object gets an access error because the file is
> already in use.
> What I'm actually trying to accomplish is far more simple: I don't actually
> want an INI file, I just want to get data that I've put into a QSettings object
> written to a QString, in the INI file format. I couldn't see any other way to
> do that than to write it out to a file, then read the file contents back into
> a QString. So if you see a simpler solution for that, I'm open to suggestions!

Well there's always QSettings::registerFormat ( 
https://doc.qt.io/qt-5/qsettings.html#registerFormat )

Haven't tried it myself but it will give you a QSettings::SettingsMap 
which means you'll still have to serialize out it yourself to a QString, 
but at least you can get rid of the file i/o by ignoring those
QIODevices that QSettings hands over. /Rgrds Henry

More information about the Interest mailing list