Unsolved How to save spinbox or sliders value into a text file ?
-
hi Qties !!
i have 6 spinboxes in my Qt designer i want to save it's values into text file !!any idea ?
thank you :p
-
Hi
Well just open file and write the values ?
You can use QString::number to go from int to text version.
Did you have something more fancy in mind ?
Also samples just writes each value after each other.
Maybe you had some format in mind ?QString filename="Data.txt"; QFile file( filename ); if ( file.open(QIODevice::ReadWrite | QIODevice::Text) ) { QTextStream stream( &file ); stream << QString::number(ui->spin1->value()) << QString::number(ui->spin2->value()) ... << endl; }
-
@mrjj
yes that is what i was thinking about , creating a file , and write into them ,
but i did better to ask before programming it , because i did not though about Qstring:: numberthank you so much , i will do it , if i get into trouble i ask again xD
-
@Amine-Djeddi
Hi
Ok super.
Do you plan to read them in again ? -
@mrjj
yes i did it before ((you adviced me to use Plain text Edit)) so i have managed to write en read into file -
@Amine-Djeddi
Ok. just asking as then you want to separate the values with newline or something like that
as as sample will look messy :)
I meant, if you wanted to read them back into the spinboxes.
If just for reading the complete text files, its not so important but might be need to be
human readable. -
@mrjj
yeees , i want to to separate the values with a comma -
@Amine-Djeddi
ok.
you can just do that directlystream << QString::number(ui->spin1->value()) <<"," << QString::number(ui->spin2->value()) <<"," .... << endl;
-
@mrjj
ahh yes sorry my english is little bit bad ..... ,
yeees i want to read them back into spinbox it's good idea !!
can you help me -
@Amine-Djeddi
Well something like
(disclaimer, didnt try compile it so there will by errors. My win qt died )QFile file("Data.txt"); if(!file.open(QIODevice::ReadOnly)) { QMessageBox::information(0, "error", file.errorString()); } QTextStream in(&file); while(!in.atEnd()) { QString line = in.readLine(); QStringList fields = line.split(","); } file.close(); Now each entry in fields will be a value ( in text format) so you can do if ( ! fields.size() ) { // this is sanity check that we did get something in list QMessageBox::information(0, "error happened", "error"); return; } // assign back ui->spin1->setValue( fields[0].toInt()); ui->spin1->setValue( fields[1].toInt());