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");
{
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();
}
}
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();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks