Results 1 to 17 of 17

Thread: Using QSettings to populate a QComboBox

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

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

    Default Re: Using QSettings to populate a QComboBox

    But do you actually write something in the ipAddressList?

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

  4. #4
    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...

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

    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(",");

  6. #6
    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
  •  
Qt is a trademark of The Qt Company.