PDA

View Full Version : QDataStream: Reading an object from disk, please advise.



kramed
27th August 2007, 03:03
Hello,

I am attempting to include binary file save/open to a simple program that I am working on. So far, it appears to be writing the data I need correctly (well, at least the file is being written and populated). The wall I am running into is loading the data back into the program. I need to save what LineEdits contain, which boxes are checked, etc. I have manually entered each element/widget using a for loop with pointers. Is there an easier way, ie. write the object data to the disk? I am not sure how to do that with Qt. If I could write as an object, hopefully I could read it as an object. Anyways, you guys know better than I. Any help or pointers to a tutorial or helpful example would be greatly appreciated.

Forgive the formatting, the paste didnt take properly.


void MainWindow::save()
{
QFile file("grades.dat");

if (!file.open(QIODevice::WriteOnly))
{
std::cout << "Cannot open file for writing: "
<< qPrintable(file.errorString()) << endl;
return;
}

QDataStream output(&file);
output.setVersion(QDataStream::Qt_4_3);

output << quint32(0x39421872);

for ( int i = 1 ; i <= 8 ; i++ )
{
output << ass_label_LineEdit_[i]->text();
output << ass_grade_SpinBox_[i]->value();
output << ass_weight_SpinBox_[i]->value();
output << ass_outof_SpinBox_[i]->value();
output << ass_check_[i]->isChecked();

output << test_label_LineEdit_[i]->text();
output << test_grade_SpinBox_[i]->value();
output << test_weight_SpinBox_[i]->value();
output << test_outof_SpinBox_[i]->value();
output << test_check_[i]->isChecked();

output << other_label_LineEdit_[i]->text();
output << other_grade_SpinBox_[i]->value();
output << other_weight_SpinBox_[i]->value();
output << other_outof_SpinBox_[i]->value();
output << other_check_[i]->isChecked();
}
}

marcel
27th August 2007, 05:29
QDataStream does not have an << operator for QString, only for const char*.
You can either write a const char instead of the edit's text or use QSettings.

Regards

kramed
27th August 2007, 05:52
Ok, thanks for the info. I will look into using QSetting.

kramed
27th August 2007, 05:59
Apparently QSettings will not be useful to me in this case as I wish for the string to be stored in the file with the other elements.

Is it possible to cast the QString to a const char and then store it in the file?

marcel
27th August 2007, 06:03
Yes, with QString::toAscii().constData().

kramed
27th August 2007, 06:04
Thank you kindly for the info.

kramed
27th August 2007, 06:07
Sorry, I forgot to ask. After I get the casting syntax properly, is it possible for me to use a similar loop to read the data back into the program? Do I have to initialize copies of the same variables and then read them in ?

marcel
27th August 2007, 06:30
Yes, it would be better to create separate vars and read the data in them.
Then you set the variables in the widgets.

Regards

jpn
27th August 2007, 15:45
QDataStream does not have an << operator for QString
QDataStream & operator<< ( QDataStream & stream, const QString & string ) (http://doc.trolltech.com/latest/qstring.html#operator-lt-lt-56)