Results 1 to 2 of 2

Thread: QSettings - Sync issue between two process

  1. #1
    Join Date
    Aug 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSettings - Sync issue between two process

    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"

    Qt Code:
    1. <product1>
    2. <subproduct1>
    3. <settings1>..</settings1>
    4. ....
    5. <settingsn>..</settingsn>
    6. </subproduct1>
    7. ...
    8. <subproductn>
    9. <settings1>..</settings1>
    10. ....
    11. <settingsn>..</settingsn>
    12. </subproductn>
    13.  
    14. </product1>
    To copy to clipboard, switch view to plain text mode 

    File: productn.xml

    Qt Code:
    1. <productn>
    2. <subproduct1>
    3. <settings1>..</settings1>
    4. ....
    5. <settingsn>..</settingsn>
    6. </subproduct1>
    7. ...
    8. <subproductn>
    9. <settings1>..</settings1>
    10. ....
    11. <settingsn>..</settingsn>
    12. </subproductn>
    13.  
    14. </productn>
    To copy to clipboard, switch view to plain text mode 

    The code in one process does the following -

    Qt Code:
    1. settings = new QSettings("product1.xml", XmlFormat);
    2. settings.setValue("settings1",<value>)
    3. sleep(20);
    4. settings.setValue("settings2", <value2>)
    5. settings.sync();
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. settings = new QSettings("product1.xml", XmlFormat);
    2. settings.remove("settings1")
    3. settings.setValue("settings3", <value3>)
    4. settings.sync();
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSettings - Sync issue between two process

    Hi,
    Qt Code:
    1. settings = new QSettings("product1.xml", XmlFormat);
    2. settings.setValue("settings1",<value>)
    3. // at this point the changes are unsaved! So your second process couldn't delete anything.
    4. // call settings.sync(); explicit.
    5. sleep(20);
    6. settings.setValue("settings2", <value2>)
    7. settings.sync();
    To copy to clipboard, switch view to plain text mode 
    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.

Similar Threads

  1. Sync Width
    By wirasto in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2009, 09:45
  2. Loading in sync
    By soxs060389 in forum Qt Programming
    Replies: 3
    Last Post: 3rd November 2009, 17:06
  3. [Qt3] Two QListviews - VerticalScrollBar Sync Problems
    By BrainB0ne in forum Qt Programming
    Replies: 0
    Last Post: 25th September 2008, 11:18
  4. how to sync a QTreeWidget and a QListWidget?
    By zl2k in forum Qt Programming
    Replies: 2
    Last Post: 5th September 2008, 20:55
  5. How to sync threads at start-up
    By Artschi in forum Qt Programming
    Replies: 5
    Last Post: 25th May 2006, 11:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.