[solved] using QSettings
-
hello everybody,
Im learning how to save data and read them back to be displayed.
I have used QSettings for this.
The text in a line edit is to be saved to a register and then display it when the class loads again.
for writing it to the register ive used the following code@
settings.beginWriteArray("notesList");
reg.note=ui->lineEdit->text();
notesList.append(reg);
for(int i=0;i<notesList.size();++i)
{
settings.setArrayIndex(i);
settings.setValue("note",notesList.at(i).note);
}for (int i = 0; i < notesList.size(); ++i) { settings.setArrayIndex(i); settings.setValue("note", notesList.at(i).note); //settings.setValue("dtHistoryDate", lDateRegistryList.at(i).dtHistoryReg); } settings.endArray();
@
and for reading it back, ive written the following code in the constructor
@
int sizeReg = settings.beginReadArray("notesList");for(int i = 0; i < sizeReg;i++) { settings.setArrayIndex(i); reg.note = settings.value("note").toString(); ui->lineEdit->setText(reg.note); }
@
Somehow this code does not work in the simulator. I cant even enter the text
help :(
alfah
-
If you just want to save and restore data I would suggest you use "QDataStream":http://doc.qt.nokia.com/latest/qdatastream.html instead, which allows for serializing almost any Qt data structure and container in a single line of code.
Besides that - is there a reason you store your values twice? Does storing any other value using QSettings work?
-
lukas,
Could you explain to me how both are different and under which situations these are used???
The module of mine requires notes to be attached to each date of a custom made calender. So when i click the button, i get another form with a line edit askin the user to input any notes if needed
I would need to attach the date to each note too.alfah
-
somebody tell me why the following code does not set value :(
@
settings.setValue("note",notesList.at(i).note);
@ -
@
struct registryStrct
{
QString note;
// QDate clickedDate;};registryStrct reg; QList<registryStrct>notesList;
@
I debugged the program and found the following
@
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);// it shows the notesList size as zero!! this is the problem!!
settings.beginReadArray("notesList"); qDebug()<<"text "<<notesList.size(); for(int i = 0; i < notesList.size() ;++i) { settings.setArrayIndex(i); reg.note = settings.value("note").toString(); qDebug()<<"text "<<reg.note; ui->lineEdit->setText(reg.note); } settings.endArray();
}
Dialog::~Dialog()
{
delete ui;
}void Dialog::on_pushButton_clicked()
{// Write data to registry settings.beginWriteArray("notesList"); reg.note=ui->lineEdit->text(); qDebug()<<"hello"<<reg.note; notesList.append(reg); qDebug()<<"hello 2"<<notesList.at(0).note ; qDebug()<<"hello 3"<<notesList.size(); for(int i=0;i<notesList.size();++i) { qDebug()<<"hello 4"<<notesList.size(); settings.setArrayIndex(i); settings.setValue("note",notesList.at(i).note); qDebug()<<"hello 5"<< settings.value("note").toString(); } settings.endArray(); MainWindow *w= new MainWindow(); w->showFullScreen();
}
@It saves the data , cannot reproduce it
-
Yes, it IS zero size, that's expected behaviour. You have error in your syntax. From the documentation:
@
QSettings settings;
int size = settings.beginReadArray("logins");
for (int i = 0; i < size; ++i) {
@and you do:
@
settings.beginReadArray("notesList");
qDebug()<<"text "<<notesList.size();
for(int i = 0; i < notesList.size() ;++i)
@Get the size in integer variable and use that for the loop.
-
ok,
somethin else, its said tht qsettings can hold all sort of settings right?
I have entered stored a date like this
@
noteSettings.setValue("setDate",notesList.at(i).setNoteDate);
@reg.setNoteDate is of QDate type
while reading it back, do we need it to use .toDate()
like this??
@
QDate checkDate=noteSettings.value("setNoteDate").toDate();
@thank you.
-
yes, there is .toDate() method for QVariant (that's what gets returned by settings.value() method).
The only trouble you could get with QSettings is if you use GUI types like QColor.
Read "here":http://doc.qt.nokia.com/stable/qsettings.html for more info -
[quote author="alfah" date="1314021081"]lukas,
Could you explain to me how both are different and under which situations these are used???[/quote]The main differences are
- sequential access for QDataStream vs random access for QSettings
- (efficient) binary storage for QDataStream vs text storage for QSettings
- non-human-readable storage for QDataStream vs human-readable storage for QSettings
There is no generally accepted rule on usage, but there is a good rule of thumb. QSettings is used to store the state of an application (opened windows, geometry) and settings (credentials, color schemes), whereas the data of an application itself is serialized using QDataStream (or another data storage like QSqlDatabase, QXmlStreamWriter).
[quote author="alfah" date="1314021081"]The module of mine requires notes to be attached to each date of a custom made calender. So when i click the button, i get another form with a line edit askin the user to input any notes if ...[/quote]
Have you considered storing the data using the "iCal standard":http://tools.ietf.org/html/rfc5545? This way your data will be interchangeable with many other applications processing calendar data.
-
will the following lines of code ensure tht Where ever i use the Qsettings, it will open up the same settings files?
@QSettings noteSettings("Plackal","Seecycles");
noteSettings.setDefaultFormat(QSettings::IniFormat);
noteSettings.setPath(QSettings::IniFormat,QSettings::UserScope,QCoreApplication::applicationDirPath());@
alfah
-