If you look at the QTableWidget documentation (http://doc.trolltech.com/4.3/qtablewidget.html) you'll see that there is a QTableWidgetItem item(int row, int column) function. So, you could easily iterate through all the cells with loops such as these:
for (int i = 0; i < rowCount(); ++i) {
if (i > 0)
out << '\n';
for (int j = 0; j < columnCount(); ++j) {
if (j > 0)
out << '\t';
out << cell->text();
}
}
QTextStream out(&file);
for (int i = 0; i < rowCount(); ++i) {
if (i > 0)
out << '\n';
for (int j = 0; j < columnCount(); ++j) {
if (j > 0)
out << '\t';
QTableWidgetItem *cell = item(i, j);
out << cell->text();
}
}
To copy to clipboard, switch view to plain text mode
this is just an example in which the program writes to a stream the contents of the table, separating each row with a newline and each column of each row with a tab.
To read from that same stream the code would be:
int numRows = rows.count();
int numColumns = rows.first().count('\t') + 1;
setRowCount(numRows);
setColumnCount(numColumns);
for (int i = 0; i < numRows; ++i) {
for (int j = 0; j < numColumns; ++j) {
cell->setText(columns[j]);
}
}
QTextStream in(&file);
QString str = in.readAll();
QStringList rows = str.split('\n');
int numRows = rows.count();
int numColumns = rows.first().count('\t') + 1;
setRowCount(numRows);
setColumnCount(numColumns);
for (int i = 0; i < numRows; ++i) {
QStringList columns = rows[i].split('\t');
for (int j = 0; j < numColumns; ++j) {
QTableWidgetItem *cell = item(i, j);
cell->setText(columns[j]);
}
}
To copy to clipboard, switch view to plain text mode
This is just an example taken from old code I had. You should adapt it to the format in which you prefer to store the data, which could even be in binary.
Bookmarks