QDataStream: Reading an object from disk, please advise.
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.
Code:
void MainWindow::save()
{
QFile file("grades.dat");
{
std::cout << "Cannot open file for writing: "
<< qPrintable(file.errorString()) << endl;
return;
}
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();
}
}
Re: QDataStream: Reading an object from disk, please advise.
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
Re: QDataStream: Reading an object from disk, please advise.
Ok, thanks for the info. I will look into using QSetting.
Re: QDataStream: Reading an object from disk, please advise.
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?
Re: QDataStream: Reading an object from disk, please advise.
Yes, with QString::toAscii().constData().
Re: QDataStream: Reading an object from disk, please advise.
Thank you kindly for the info.
Re: QDataStream: Reading an object from disk, please advise.
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 ?
Re: QDataStream: Reading an object from disk, please advise.
Yes, it would be better to create separate vars and read the data in them.
Then you set the variables in the widgets.
Regards
Re: QDataStream: Reading an object from disk, please advise.
Quote:
Originally Posted by
marcel
QDataStream does not have an << operator for QString
QDataStream & operator<< ( QDataStream & stream, const QString & string )