You can iterate through the ItemModel (instead of table widget items). Here a some what generic code, with which you can save & restore all item data roles. With a minor modifications and use of recursion this code can also be used to save TreeWidgets
void Victims::load()
{
for(int row = 0; row < model->rowCount(); row++)
{
settings
->beginGroup
(QString("Row-%1").
arg(row
));
for(int col = 0; col < model->columnCount(); col++)
{
settings
->beginGroup
(QString("Column-%1").
arg(col
));
for(int role = 0; role < Qt::UserRole; role++)
{
model
->setData
(item, settings
->value
(QString("ItemDataRole-%1").
arg(role
)));
// Restore from file }
settings->endGroup();
}
settings->endGroup();
}
}
void Victims::save()
{
for(int row = 0; row < model->rowCount(); row++)
{
settings
->beginGroup
(QString("Row-%1").
arg(row
));
for(int col = 0; col < model->columnCount(); col++)
{
settings
->beginGroup
(QString("Column-%1").
arg(col
));
for(int role = 0; role < Qt::UserRole; role++)
{
settings
->setValue
(QString("ItemDataRole-%1").
arg(role
), model
->data
(item, role
));
// Save to file }
settings->endGroup();
}
settings->endGroup();
}
}
void Victims::load()
{
QAbstractItemModel* model = ui->tw->model();
for(int row = 0; row < model->rowCount(); row++)
{
settings->beginGroup(QString("Row-%1").arg(row));
for(int col = 0; col < model->columnCount(); col++)
{
settings->beginGroup(QString("Column-%1").arg(col));
QModelIndex item = model->index(row, col);
for(int role = 0; role < Qt::UserRole; role++)
{
model->setData(item, settings->value(QString("ItemDataRole-%1").arg(role))); // Restore from file
}
settings->endGroup();
}
settings->endGroup();
}
}
void Victims::save()
{
QAbstractItemModel* model = ui->tw->model();
for(int row = 0; row < model->rowCount(); row++)
{
settings->beginGroup(QString("Row-%1").arg(row));
for(int col = 0; col < model->columnCount(); col++)
{
settings->beginGroup(QString("Column-%1").arg(col));
QModelIndex item = model->index(row, col);
for(int role = 0; role < Qt::UserRole; role++)
{
settings->setValue(QString("ItemDataRole-%1").arg(role), model->data(item, role)); // Save to file
}
settings->endGroup();
}
settings->endGroup();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks