Results 1 to 5 of 5

Thread: QSettings again ... how to remove array elements

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    52
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSettings again ... how to remove array elements

    Hi all,

    I would like to store some data structured as an array. That's working, and my settings file contains something like:
    Qt Code:
    1. [blogAccounts]
    2. 1\accountName=Test 1
    3. 1\userName=Mike
    4. 1\userPassword=secret
    5. 2\accountName=Test 2
    6. 2\userName=Mike
    7. 2\userPassword=secret
    8. size=2
    To copy to clipboard, switch view to plain text mode 

    Now I use this code to remove the first item, and let's assume that the currentAccountName contains the first entry of the array:
    Qt Code:
    1. ...
    2. // Ok, we need to find the right account...
    3. QSettings settings("MKrueger", "MKBlog");
    4. int size = settings.beginReadArray("blogAccounts");
    5. bool didRemove = false;
    6. for (int i = 0; i < size; ++i)
    7. {
    8. // Select the data set
    9. settings.setArrayIndex(i);
    10. // Now compare the account name with the one selected...
    11. if (_currentAccountName == settings.value("accountName").toString())
    12. {
    13. // This is the one we need to remove...
    14. settings.endArray();
    15. // Now reopen the settings for writing...
    16. settings.beginWriteArray("blogAccounts");
    17. // select the desired account again
    18. settings.setArrayIndex(i);
    19. // and now remove it...
    20. settings.remove("");
    21. didRemove = true;
    22. break;
    23. }
    24. }
    25. settings.endArray();
    26.  
    27. if (false == didRemove)
    28. return; // Unable to remove account
    To copy to clipboard, switch view to plain text mode 

    The result in my settings file is this:
    Qt Code:
    1. [blogAccounts]
    2. 2\accountName=Test 2
    3. 2\userName=Mike
    4. 2\userPassword=secret
    5. size=1
    To copy to clipboard, switch view to plain text mode 

    If I now try to load the remaining account it fails. I suspect because the prefix is still 2 and not 1. Would that assumption be correct?
    If so, how should I remove an item correctly? Do I have to remove the complete array and then write it out again?

    Thanks...

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    52
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings again ... how to remove array elements

    In addition to the above, I also found out, that if I remove the last array item, then the size value will still be 1, and not 0 - which I would have expected.
    I start to believe I should use something else for storing my data, or am I doing something completely wrong here?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings again ... how to remove array elements

    Why not rewrite the array from the beginning so that indexes start from the beginning (1) again?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    52
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings again ... how to remove array elements

    ... well yes, I guess that's a solution. I just thought that if QSettings offers the option to remove items from the array, then it should handle it the right way. Since this doesn't seem to be the case, I would consider this a bug...
    I guess what I need to do is to remove the whole section (or group). Hopefully that will work, and then write out a new array as you suggested. I will try that later today...

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings again ... how to remove array elements

    It doesn't offer removing an item from array, it offers removing an item (in general). Array differs from "regular" elements only that QSettings has a "smart" function to operate on them but they are really usual entries.

Similar Threads

  1. Concatenate two array elements
    By b1 in forum General Programming
    Replies: 6
    Last Post: 29th October 2007, 21:43
  2. Remove first n elements from QList
    By pakulo in forum Qt Programming
    Replies: 8
    Last Post: 4th June 2007, 08:27

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.