You should walk through the model and save on an item-by-item basis. You may open a file and use QDataStream to save the contents of each item. For example, assuming the model is a table model (for simplicity):
{
int rowCount, columnCount;
stream >> rowCount;
stream >> columnCount;
for(int row = 0; row < rowCount; row++)
for(int column = 0; column < columnCount; column++) {
stream >> item;
model->setItem(row, column, item);
}
}
{
int rowCount = model->rowCount();
int columnCount = model->columnCount();
stream << rowCount;
stream << columnCount;
for(int row = 0; row < rowCount; row++)
for(int column = 0; column < columnCount; column++) {
stream << model->item(row, column)->text();
}
}
...
/* populate the model */
saveModel(&file, &myModel);
void loadModel(QIODevice *device, QStandardItemModel *model)
{
QDataStream stream(device);
int rowCount, columnCount;
stream >> rowCount;
stream >> columnCount;
for(int row = 0; row < rowCount; row++)
for(int column = 0; column < columnCount; column++) {
QString item;
stream >> item;
model->setItem(row, column, item);
}
}
void saveModel(QIODevice *device, QStandardItemModel *model)
{
QDataStream stream(device);
int rowCount = model->rowCount();
int columnCount = model->columnCount();
stream << rowCount;
stream << columnCount;
for(int row = 0; row < rowCount; row++)
for(int column = 0; column < columnCount; column++) {
stream << model->item(row, column)->text();
}
}
...
QStandardItemModel myModel;
/* populate the model */
QFile file;
if(file.open(QIODevice::WriteOnly))
saveModel(&file, &myModel);
To copy to clipboard, switch view to plain text mode
Bookmarks