Results 1 to 4 of 4

Thread: QSettings size does not change on adding new array element

  1. #1
    Join Date
    Jan 2011
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSettings size does not change on adding new array element

    Dear All,
    I am using QSettings for creating and adding config file. And I am using Array element. I am adding and deleting array elements programatically. I am able to add/delete that array element from config file but doing so it does not change the size of the array. Next time I run the program I cannot see the changes in size.

    sizeOfArray = pSettings->beginReadArray("ArrayName");

    This is because adding or deleting elements in the config file does not change the value "size __ " automatically. Does anybody know how to change the size of QSettings Array. Currently I am doing it manually by reading the file using QFile, but I don't like it.

    Please let me know if you know something in this regard.

    Thank you in advance,
    Amita

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings size does not change on adding new array element

    This is because adding or deleting elements in the config file does not change the value "size __ " automatically.
    What do you means for "adding or deleting elements in the config file"?

    For me it works.

    This code
    Qt Code:
    1. void EditorWidget::writeSettings()
    2. {
    3. QSettings settings;
    4.  
    5. QTextDocument *doc = ui->plainTextEdit->document ();
    6. QTextBlock block;
    7.  
    8. settings.beginGroup ("editor");
    9. settings.beginWriteArray ("contents");
    10. for (int i = 0; i < doc->blockCount (); ++i) {
    11. settings.setArrayIndex (i);
    12. block = doc->findBlockByNumber (i);
    13. settings.setValue ("text", block.text ());
    14. }
    15.  
    16. settings.endArray ();
    17. settings.endGroup ();
    18. }
    To copy to clipboard, switch view to plain text mode 

    save the contents of a QPlainTextEdit line by line
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    May 2010
    Posts
    24
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings size does not change on adding new array element

    Hi,

    QSettings doesn't really support editing arrays, and doesn't even check that the indexes are contiguous. It just takes the maximum index passed to setArrayIndex, adds 1, and writes it as the size.
    To have a clean array without leftovers or hole between the indexes, you have to read it to memory, remove the whole array and rewrite it entirely.

  4. #4
    Join Date
    Jan 2011
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSettings size does not change on adding new array element

    Thank you guys for the answer. At the end I also did the same. Whenever I need to edit the array, I am deleting it completely and rewriting it.

  5. The following user says thank you to Amita for this useful post:

    frankiefrank (17th April 2012)

Similar Threads

  1. Can't change form element names after copying them
    By Computer Hater in forum Qt Tools
    Replies: 0
    Last Post: 8th July 2011, 04:24
  2. Adding element to QTableWidget.
    By sudhansu in forum Qt Programming
    Replies: 0
    Last Post: 6th March 2010, 08:16
  3. Change a QList element
    By mcrahr in forum Newbie
    Replies: 1
    Last Post: 23rd August 2009, 12:16
  4. silly array element counter..
    By ct in forum General Programming
    Replies: 4
    Last Post: 3rd March 2006, 16:50
  5. QSettings again ... how to remove array elements
    By Mike in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 09:58

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.