Results 1 to 6 of 6

Thread: QSettings problem

  1. #1
    Join Date
    Nov 2006
    Posts
    96

    Default QSettings problem

    Hi, I have a problem writting and reading QSettings...

    Qt Code:
    1. void clientGui::read_settings() {
    2. QSettings settings(COMPANY, PRODUCT);
    3. tab_counter = settings.value("counter").toInt();
    4. for(quint32 i=0;i<tab_counter;i++) {
    5. tab_names.append(settings.value("tab"+tab_counter).toString());
    6. }
    7. }
    8.  
    9. void clientGui::write_settings() {
    10. /* TODO: spremeni ti dve imeni v dejanski imeni: ime_firme : ime_programa */
    11. QSettings settings(COMPANY, PRODUCT);
    12.  
    13. /* counter - number of additional tabs */
    14. settings.setValue("counter", tab_counter);
    15.  
    16. /* save the schema names */
    17. settings.setValue("tab"+tab_counter, tab_name);
    18. }
    19.  
    20. void clientGui::change_settings(QString name, bool read_write) {
    21. QSettings settings(COMPANY, PRODUCT);
    22.  
    23. /* i++ the counter - add one new tab */
    24. if(read_write) {
    25. tab_counter += 1;
    26. /* save the tab name */
    27. settings.setValue("tab"+tab_counter, name);
    28. }
    29. else if(tab_counter > 0) {
    30. tab_counter -= 1;
    31.  
    32. /* remove the name */
    33. settings.remove(name);
    34. }
    35. /* save the counter */
    36. settings.setValue("counter", tab_counter);
    37.  
    38. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that my settings look like this:
    [General]
    ab=fadf
    counter=0
    b=erf
    tab=
    where it should look like this:
    counter=0
    tab0=X
    tab1=X
    tab2=X ... etc (where X is some name)

    change_settings should add new tab[number] variable with a name...

    What is the problem...any ideas?

  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 problem

    Quote Originally Posted by eleanor View Post
    Qt Code:
    1. settings.setValue("tab"+tab_counter, tab_name);
    To copy to clipboard, switch view to plain text mode 
    Instead of doing it that way, you might have a look at QSettings::beginWriteArray().

  3. #3
    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 problem

    Probably the Error is that QString:perator+ doesn't accept numeric values.
    You can use
    Qt Code:
    1. settings.setValue("tab" + QString::number(tab_counter), tab_name);
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. settings.setValue(QString("tab").arg(tab_counter), tab_name);
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSettings problem

    what's more, this code:
    Qt Code:
    1. for(quint32 i=0;i<tab_counter;i++) {
    2. tab_names.append(settings.value(QString("tab%1").arg(tab_counter)).toString());
    3. }
    To copy to clipboard, switch view to plain text mode 
    for 10 tabs will append the "tab10" 10 times, because in every loop pass tab_counter is constant and equal 10, so I think you want to put i in arg() and not tab_counter.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    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 problem

    Qt Code:
    1. settings.setValue(QString("tab%1").arg(tab_counter), tab_name);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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 problem

    Sorry!!

    But this
    Qt Code:
    1. settings.setValue(QString("tab%1").arg(i), tab_name);
    To copy to clipboard, switch view to plain text mode 
    can be better
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Problem with QSettings
    By arbi in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2008, 08:49
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  4. QSettings problem?
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 28th June 2008, 21:14
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.