PDA

View Full Version : QSettings - Sync issue between two process



kartlee
26th August 2011, 19:25
I am using Qsettings for non gui products to store its settings into xml files. This is written as a library which gets used in C, C++ programs. There will be 1 xml file file for each product. Each product might have more than one sub products and they are written into xml by subproduct grouping as follows -

File: "product1.xml"



<product1>
<subproduct1>
<settings1>..</settings1>
....
<settingsn>..</settingsn>
</subproduct1>
...
<subproductn>
<settings1>..</settings1>
....
<settingsn>..</settingsn>
</subproductn>

</product1>


File: productn.xml



<productn>
<subproduct1>
<settings1>..</settings1>
....
<settingsn>..</settingsn>
</subproduct1>
...
<subproductn>
<settings1>..</settings1>
....
<settingsn>..</settingsn>
</subproductn>

</productn>


The code in one process does the following -



settings = new QSettings("product1.xml", XmlFormat);
settings.setValue("settings1",<value>)
sleep(20);
settings.setValue("settings2", <value2>)
settings.sync();


When the first process goes to sleep, I start another process which does the following -



settings = new QSettings("product1.xml", XmlFormat);
settings.remove("settings1")
settings.setValue("settings3", <value3>)
settings.sync();


I would expect the settings1 to go away from product1.xml file but it still persist in the file - product1.xml at the end of above two process. I am not using QCoreApplication(..) in my settings library. Please point issues if there is anything wrong in the above design.

From documentation I see -

"
QSettings can safely be used from different processes (which can be different instances of your application running at the same time or different applications altogether) to read and write to the same system locations. It uses advisory file locking and a smart merging algorithm to ensure data integrity. Note that sync() imports changes made by other processes (in addition to writing the changes from this QSettings).
"

Based on the above note, I assume 'settings' to be removed from the file finally.

-Kartlee

Lykurg
26th August 2011, 22:19
Hi,

settings = new QSettings("product1.xml", XmlFormat);
settings.setValue("settings1",<value>)
// at this point the changes are unsaved! So your second process couldn't delete anything.
// call settings.sync(); explicit.
sleep(20);
settings.setValue("settings2", <value2>)
settings.sync();
Furthermore it is normally usual to create a QSettings object on the stack whenever it is needed. Thus it gets destroyed after the scope and calls sync automatically.