[SOLVED] saving and loading QTableWidgets
-
EDIT 2: I figured out the second problem! my iteration was out of whack. I fixed it by first setting a new item for each cell of the new table, right after setting its rowcount and column count and before going through the loaded string list, and I set each item's text to a place holder. Only after initializing the whole table did I go into iterating through the string. dunno why my problem code didin't work, but whatever, this works :)
I have two table widgets, table_widget and table_widget_2 - one rowed "tape". I haven't subclassed anything except for the delegate for the tape one. I'm saving both of them in a very similar way, but getting very different results.
saving and opening the first one:
@void MainWindow::saveShema()
{
QString fileName = QFileDialog::getSaveFileName(this,
QString("Spremanje..."), QString(""),
QString("Funkcionalna shema (.tsf);;Sve datoteke(.*)"));if (fileName.isEmpty()) { return; } else { QFile file(fileName); if (!file.open(QIODevice::WriteOnly)) { QMessageBox::information(this, QString("Greška!"), QString("Datoteka se ne može otvoriti"), QMessageBox::Ok); return; } QDataStream out(&file); out.setVersion(QDataStream::Qt_4_5); QStringList saveData; for(int row=0; row<symbols.size(); row++) //symbols and states are qstringlists and are directly related to //the widget's rowcount and columncount, symbols are for the rows and states are for the columns { for(int column=0; column<states.size(); column++) { if(!ui->tableWidget->item(row, column)||ui->tableWidget->item(row, column)->text().isEmpty()) { saveData.append(" "); } else { saveData.append(ui->tableWidget->item(row, column)->text()); } } } //done iterating, saving out<<states<<symbols<<saveData; file.close(); unsaved=false; }
}
@
@void MainWindow::openShema()
{QString fileName = QFileDialog::getOpenFileName(this, QString("Otvaranje..."), QString(""), QString("Funkcionalna shema (*.tsf);;Sve datoteke (*.*)")); if (fileName.isEmpty()) { return; } else { QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { QMessageBox::information(this, QString("Greška!"), QString("Datoteka se ne može otvoriti"), QMessageBox::Ok); return; } QDataStream in(&file); in.setVersion(QDataStream::Qt_4_5); QStringList loadData; states.clear(); symbols.clear(); ui->tableWidget->clear(); in>>states>>symbols>>loadData; ui->tableWidget->setColumnCount(states.size()); ui->tableWidget->setRowCount(symbols.size()); for(int row=0; row<symbols.size(); row++) { for(int column=0; column<states.size(); column++) { if(!ui->tableWidget->item(row, column)) { QTableWidgetItem *item = new QTableWidgetItem(); item->setText(loadData.at(row*states.size()+column)); } else { ui->tableWidget->item(row, column)->setText(loadData.at(row*states.size()+column)); } } } file.close(); unsaved=false; }
}
@
this one saves something, but I can't get all of it out of the file. It loads the symbols and states just fne, sets the row and column counts, but wipes all the data I previously had in it and sets nothing.