PDA

View Full Version : Using QSettings to populate a QComboBox



nbkhwjm
9th July 2007, 22:42
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..



void Porcupine::readSettings()
{

QSettings settings(QSettings::IniFormat, QSettings::UserScope,"software", "app1");

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()
{

QSettings settings(QSettings::IniFormat, QSettings::UserScope,"software", "app1");

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

Lykurg
10th July 2007, 07:20
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.
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

rajesh
10th July 2007, 08:14
try this code:


void Porcupine::readSettings()
{
...
settings.beginGroup("network");
QStringList ipAddressList = settings.value("ipaddress").split(",");
settings.endGroup();

}
void Porcupine::saveSettings()
{
...
settings.beginGroup("network");
QStringList ipAddressList;
for(int index = 0; index < ui.ipaddressComboBox.count();index++)
ipAddressList.append(ui.ipaddressComboBox.itemText (index));
settings.setValue("ipaddress", ipAddressList.join(","));
settings.endGroup();

}

nbkhwjm
17th July 2007, 17:46
Thanks for the assistance..

I get this error on compilation...

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

jpn
17th July 2007, 18:42
So convert the variant to a string list with QVariant::toStringList().

nbkhwjm
17th July 2007, 20:36
Im sorry to say I don't understand how to do that..

Hence me posting in the newbie Forum ;^)

nbkhwjm
17th July 2007, 20:43
is this what you mean?


QStringList ipAddressList = settings.value("ipaddress").toStringList();

jpn
17th July 2007, 20:59
Sorry, I just noticed that for some reason there's an unnecessary step (joining and splitting a string):


// write
settings.setValue("ipaddress", ipAddressList); // no need to .join(",")

// read
QStringList ipAddressList = settings.value("ipaddress").toStringList();

rajesh
18th July 2007, 05:01
here settings.value will return QString, So you store in QString, later you split and store in QStringList.


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

jpn
18th July 2007, 07:34
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.

rajesh
18th July 2007, 07:40
Jpn,
you are right but I simply explained my previous code.

nbkhwjm
13th August 2007, 23:05
Here is what I have so far... Its still not working...


void Porcupine::readSettings()
{

QSettings settings(QSettings::IniFormat, QSettings::UserScope,"Codematic", "Porcupine");

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()
{

QSettings settings(QSettings::IniFormat, QSettings::UserScope,"Codematic", "Porcupine");

QStringList ipAddressList;

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:


[MainWindow]
pos=@Point(174 0)
size=@Size(785 480)

[network]
filename=@Invalid()
ipaddress=@Invalid()

marcel
13th August 2007, 23:17
But do you actually write something in the ipAddressList?

nbkhwjm
14th August 2007, 03:43
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?

nbkhwjm
14th August 2007, 04:06
Ok I can make it populate the Ini file now... with this:


void Porcupine::saveSettings()
{

QSettings settings(QSettings::IniFormat, QSettings::UserScope,"Codematic", "Porcupine");

QStringList ipAddressList;
QStringList fileNameList;

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)

[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...

rajesh
14th August 2007, 06:31
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(",");

nbkhwjm
4th September 2007, 22:34
Update to this issue...

This works perfectly...

Save Settings...

QStringList ipAddressList;
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 ..

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


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.