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..
Code:
void Porcupine::readSettings()
{
settings.beginGroup("MainWindow");
resize
(settings.
value("size",
QSize(800,
400)).
toSize());
move
(settings.
value("pos",
QPoint(200,
200)).
toPoint());
settings.endGroup();
settings.beginGroup("network");
// The connection details go here
settings.endGroup();
ui.statusLogs->append(tr("Settings Restored"));
}
// Saves in (linux) $home/.config/software/app1.ini
void Porcupine::saveSettings()
{
settings.beginGroup("MainWindow");
settings.setValue("size", size());
settings.setValue("pos", pos());
settings.endGroup();
settings.beginGroup("network");
settings.setValue("ipaddress", ui.ipaddressComboBox->currentText());
settings.setValue("printerPortNumber", ui.printerPortNumber->text());
settings.endGroup();
ui.statusLogs->append(tr("Settings Saved"));
}
Thanks for all the help.
Chris
Re: Using QSettings to populate a QComboBox
Quote:
Originally Posted by
nbkhwjm
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.
Code:
settings.setValue("ipaddress", ui.ipaddressComboBox->currentText());
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
Re: Using QSettings to populate a QComboBox
try this code:
Code:
void Porcupine::readSettings()
{
...
settings.beginGroup("network");
QStringList ipAddressList
= settings.
value("ipaddress").
split(",");
settings.endGroup();
}
void Porcupine::saveSettings()
{
...
settings.beginGroup("network");
for(int index = 0; index < ui.ipaddressComboBox.count();index++)
ipAddressList.append(ui.ipaddressComboBox.itemText(index));
settings.setValue("ipaddress", ipAddressList.join(","));
settings.endGroup();
}
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'
Re: Using QSettings to populate a QComboBox
So convert the variant to a string list with QVariant::toStringList().
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 ;^)
Re: Using QSettings to populate a QComboBox
is this what you mean?
Code:
QStringList ipAddressList
= settings.
value("ipaddress").
toStringList();
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):
Code:
// write
settings.setValue("ipaddress", ipAddressList); // no need to .join(",")
// read
QStringList ipAddressList
= settings.
value("ipaddress").
toStringList();
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.
Code:
QString tempStr
= settings.
value("ipaddress");
Re: Using QSettings to populate a QComboBox
Quote:
Originally Posted by
rajesh
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.
Re: Using QSettings to populate a QComboBox
Jpn,
you are right but I simply explained my previous code.
Re: Using QSettings to populate a QComboBox
Here is what I have so far... Its still not working...
Code:
void Porcupine::readSettings()
{
settings.beginGroup("MainWindow");
resize
(settings.
value("size",
QSize(800,
400)).
toSize());
move
(settings.
value("pos",
QPoint(200,
200)).
toPoint());
settings.endGroup();
settings.beginGroup("network");
QStringList ipAddressList
= settings.
value("ipaddress").
toStringList();
settings.endGroup();
ui.statusLogs->append(tr("Settings Restored"));
}
// Saves in (linux) $home/.config/Codematic/Porcupine.ini
void Porcupine::saveSettings()
{
settings.beginGroup("MainWindow");
settings.setValue("size", size());
settings.setValue("pos", pos());
settings.endGroup();
settings.beginGroup("network");
settings.setValue("ipaddress", ipAddressList);
settings.endGroup();
ui.statusLogs->append(tr("Settings Saved"));
}
But all I get in the Settings file is this:
Code:
[MainWindow]
pos=@Point(174 0)
size=@Size(785 480)
[network]
filename=@Invalid()
ipaddress=@Invalid()
Re: Using QSettings to populate a QComboBox
But do you actually write something in the ipAddressList?
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?
Re: Using QSettings to populate a QComboBox
Ok I can make it populate the Ini file now... with this:
Code:
void Porcupine::saveSettings()
{
settings.beginGroup("MainWindow");
settings.setValue("size", size());
settings.setValue("pos", pos());
settings.endGroup();
settings.beginGroup("network");
for(int index = 0; index < ui.ipaddressComboBox->count();index++)
ipAddressList.append(ui.ipaddressComboBox->itemText(index));
settings.setValue("ipaddressComboBox", ipAddressList);
settings.setValue("filename", fileNameList);
settings.endGroup();
ui.statusLogs->append(tr("Settings Saved"));
}
I get this in the INI, which is good (i think)
Code:
[MainWindow]
pos=@Point(107 71)
size=@Size(785 480)
[network]
filename=@Invalid()
ipaddressComboBox=1.1.1.1, 2.2.2.2, 3.3.3.3
Now working getting it to read and populate the combo box...
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(",");
Re: Using QSettings to populate a QComboBox
Update to this issue...
This works perfectly...
Save Settings...
Code:
for(int index = 0; index < ui.ipaddressComboBox->count();index++)
ipAddressList.append(ui.ipaddressComboBox->itemText(index));
settings.setValue("IpAddressHistory", ipAddressList);
Writes this to the INI file ..
Code:
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
read it with this...
Code:
QStringList ipAddressList
= settings.
value("IpAddressHistory").
toStringList();
if (!ipAddressList.isEmpty())
{
for(int i = 0; i < ipAddressList.count();i++)
{
ui.ipaddressComboBox->addItem(ipAddressList[i]);
}
}
Thanks to all who provided insight.