Results 1 to 17 of 17

Thread: Using QSettings to populate a QComboBox

  1. #1
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Using QSettings to populate a QComboBox

    All,

    I have been fighting with this for soem time, and I'd thought i'd ask.

    I have an app that has a QComboBox that holds the last 5 ipaddresses used. I want to save the contents of this with Qsettings to INI file and read them back in.

    I have a readSettings and a saveSettings that creates the INI file, but only writes the current entry in the file, not all the past ones.

    I have looked in the docs but it only shows how to use setValue and Value on QLineEdit, not QComboBox.

    Any ideas or sample code you can point me to?

    Here are the read and save functions, but the Combo box bits are removed. I coulnd not even get close to a workable solution for it..

    Qt Code:
    1. void Porcupine::readSettings()
    2. {
    3.  
    4. QSettings settings(QSettings::IniFormat, QSettings::UserScope,"software", "app1");
    5.  
    6. settings.beginGroup("MainWindow");
    7. resize(settings.value("size", QSize(800, 400)).toSize());
    8. move(settings.value("pos", QPoint(200, 200)).toPoint());
    9. settings.endGroup();
    10.  
    11. settings.beginGroup("network");
    12. // The connection details go here
    13. settings.endGroup();
    14.  
    15. ui.statusLogs->append(tr("Settings Restored"));
    16.  
    17.  
    18. }
    19.  
    20. // Saves in (linux) $home/.config/software/app1.ini
    21.  
    22. void Porcupine::saveSettings()
    23. {
    24.  
    25. QSettings settings(QSettings::IniFormat, QSettings::UserScope,"software", "app1");
    26.  
    27. settings.beginGroup("MainWindow");
    28. settings.setValue("size", size());
    29. settings.setValue("pos", pos());
    30. settings.endGroup();
    31.  
    32. settings.beginGroup("network");
    33. settings.setValue("ipaddress", ui.ipaddressComboBox->currentText());
    34. settings.setValue("printerPortNumber", ui.printerPortNumber->text());
    35. settings.endGroup();
    36.  
    37. ui.statusLogs->append(tr("Settings Saved"));
    38.  
    39. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for all the help.

    Chris

  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: Using QSettings to populate a QComboBox

    Quote Originally Posted by nbkhwjm View Post
    I have a readSettings and a saveSettings that creates the INI file, but only writes the current entry in the file, not all the past ones.
    Qt Code:
    1. settings.setValue("ipaddress", ui.ipaddressComboBox->currentText());
    To copy to clipboard, switch view to plain text mode 
    Of course you only get the current selected text when you use currentText(). To get all item texts in the combobox iterate through the box and fetch the strings using "QString itemText ( int index )".


    Lykurg

  3. #3
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Using QSettings to populate a QComboBox

    try this code:
    Qt Code:
    1. void Porcupine::readSettings()
    2. {
    3. ...
    4. settings.beginGroup("network");
    5. QStringList ipAddressList = settings.value("ipaddress").split(",");
    6. settings.endGroup();
    7.  
    8. }
    9. void Porcupine::saveSettings()
    10. {
    11. ...
    12. settings.beginGroup("network");
    13. QStringList ipAddressList;
    14. for(int index = 0; index < ui.ipaddressComboBox.count();index++)
    15. ipAddressList.append(ui.ipaddressComboBox.itemText(index));
    16. settings.setValue("ipaddress", ipAddressList.join(","));
    17. settings.endGroup();
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 10th July 2007 at 12:59. Reason: changed [html] to [code]

  4. #4
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Using QSettings to populate a QComboBox

    Thanks for the assistance..

    I get this error on compilation...

    error: 'class QVariant' has no member named 'split'

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Using QSettings to populate a QComboBox

    So convert the variant to a string list with QVariant::toStringList().
    J-P Nurmi

  6. #6
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Using QSettings to populate a QComboBox

    Im sorry to say I don't understand how to do that..

    Hence me posting in the newbie Forum ;^)

  7. #7
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Using QSettings to populate a QComboBox

    is this what you mean?

    Qt Code:
    1. QStringList ipAddressList = settings.value("ipaddress").toStringList();
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Using QSettings to populate a QComboBox

    Sorry, I just noticed that for some reason there's an unnecessary step (joining and splitting a string):
    Qt Code:
    1. // write
    2. settings.setValue("ipaddress", ipAddressList); // no need to .join(",")
    3.  
    4. // read
    5. QStringList ipAddressList = settings.value("ipaddress").toStringList();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. #9
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Using QSettings to populate a QComboBox

    here settings.value will return QString, So you store in QString, later you split and store in QStringList.
    Qt Code:
    1. QString tempStr = settings.value("ipaddress");
    2. QStringList ipAddressList = tempStr.split(",");
    To copy to clipboard, switch view to plain text mode 
    Last edited by rajesh; 18th July 2007 at 05:03. Reason: correction of code, changing correct case

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Using QSettings to populate a QComboBox

    Quote Originally Posted by rajesh View Post
    here settings.value will return QString, So you store in QString, later you split and store in QStringList.
    The question is, why? QSettings is capable of storing a QVariant whereas QVariant is capable of holding a QStringList. No joining and splitting are needed at all.
    J-P Nurmi

  11. #11
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Using QSettings to populate a QComboBox

    Jpn,
    you are right but I simply explained my previous code.

  12. #12
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Using QSettings to populate a QComboBox

    Here is what I have so far... Its still not working...

    Qt Code:
    1. void Porcupine::readSettings()
    2. {
    3.  
    4. QSettings settings(QSettings::IniFormat, QSettings::UserScope,"Codematic", "Porcupine");
    5.  
    6. settings.beginGroup("MainWindow");
    7. resize(settings.value("size", QSize(800, 400)).toSize());
    8. move(settings.value("pos", QPoint(200, 200)).toPoint());
    9. settings.endGroup();
    10.  
    11. settings.beginGroup("network");
    12. QStringList ipAddressList = settings.value("ipaddress").toStringList();
    13. settings.endGroup();
    14.  
    15. ui.statusLogs->append(tr("Settings Restored"));
    16.  
    17.  
    18. }
    19.  
    20. // Saves in (linux) $home/.config/Codematic/Porcupine.ini
    21.  
    22. void Porcupine::saveSettings()
    23. {
    24.  
    25. QSettings settings(QSettings::IniFormat, QSettings::UserScope,"Codematic", "Porcupine");
    26.  
    27. QStringList ipAddressList;
    28.  
    29. settings.beginGroup("MainWindow");
    30. settings.setValue("size", size());
    31. settings.setValue("pos", pos());
    32. settings.endGroup();
    33.  
    34. settings.beginGroup("network");
    35. settings.setValue("ipaddress", ipAddressList);
    36. settings.endGroup();
    37.  
    38. ui.statusLogs->append(tr("Settings Saved"));
    39.  
    40. }
    To copy to clipboard, switch view to plain text mode 

    But all I get in the Settings file is this:

    Qt Code:
    1. [MainWindow]
    2. pos=@Point(174 0)
    3. size=@Size(785 480)
    4.  
    5. [network]
    6. filename=@Invalid()
    7. ipaddress=@Invalid()
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QSettings to populate a QComboBox

    But do you actually write something in the ipAddressList?

  14. #14
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Using QSettings to populate a QComboBox

    Im not quite sure how to confirm that, but the comboBox has values in it when the gui comes up from ui.ipaddressComboBox->AddItem("value") and others that are added while the app is running...

    when I run the save function other settings are placed in the file, but only "Invalid()" the item for ipaddress..

    Is there another way to verify is ipaddressList is populated?

  15. #15
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Default Re: Using QSettings to populate a QComboBox

    Ok I can make it populate the Ini file now... with this:

    Qt Code:
    1. void Porcupine::saveSettings()
    2. {
    3.  
    4. QSettings settings(QSettings::IniFormat, QSettings::UserScope,"Codematic", "Porcupine");
    5.  
    6. QStringList ipAddressList;
    7. QStringList fileNameList;
    8.  
    9. settings.beginGroup("MainWindow");
    10. settings.setValue("size", size());
    11. settings.setValue("pos", pos());
    12. settings.endGroup();
    13.  
    14. settings.beginGroup("network");
    15. for(int index = 0; index < ui.ipaddressComboBox->count();index++)
    16. ipAddressList.append(ui.ipaddressComboBox->itemText(index));
    17. settings.setValue("ipaddressComboBox", ipAddressList);
    18. settings.setValue("filename", fileNameList);
    19. settings.endGroup();
    20.  
    21. ui.statusLogs->append(tr("Settings Saved"));
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    I get this in the INI, which is good (i think)
    Qt Code:
    1. [MainWindow]
    2. pos=@Point(107 71)
    3. size=@Size(785 480)
    4.  
    5. [network]
    6. filename=@Invalid()
    7. ipaddressComboBox=1.1.1.1, 2.2.2.2, 3.3.3.3
    To copy to clipboard, switch view to plain text mode 


    Now working getting it to read and populate the combo box...

  16. #16
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Using QSettings to populate a QComboBox

    yes, writing ini file content is correct like
    ipaddressComboBox=1.1.1.1, 2.2.2.2, 3.3.3.3
    now you can read as
    QStringList ipAddressList = settings.value("ipaddress").toStringList();

    OR

    QString tempStr = settings.value("ipaddress");
    QStringList ipAddressList = tempStr.split(",");

  17. #17
    Join Date
    Mar 2007
    Posts
    59
    Thanks
    7

    Smile Re: Using QSettings to populate a QComboBox

    Update to this issue...

    This works perfectly...

    Save Settings...
    Qt Code:
    1. QStringList ipAddressList;
    2. for(int index = 0; index < ui.ipaddressComboBox->count();index++)
    3. ipAddressList.append(ui.ipaddressComboBox->itemText(index));
    4. settings.setValue("IpAddressHistory", ipAddressList);
    To copy to clipboard, switch view to plain text mode 

    Writes this to the INI file ..
    Qt Code:
    1. IpAddressHistory=192.168.1.65, 192.168.2.52, 127.0.0.1, 192.192.192.192, 192.168.1.65, 192.168.1.65
    To copy to clipboard, switch view to plain text mode 

    read it with this...

    Qt Code:
    1. QStringList ipAddressList = settings.value("IpAddressHistory").toStringList();
    2.  
    3. if (!ipAddressList.isEmpty())
    4. {
    5. for(int i = 0; i < ipAddressList.count();i++)
    6. {
    7. ui.ipaddressComboBox->addItem(ipAddressList[i]);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Thanks to all who provided insight.

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.