Quote Originally Posted by Santosh Reddy View Post
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

Qt Code:
  1. void Victims::load()
  2. {
  3. QAbstractItemModel* model = ui->tw->model();
  4. for(int row = 0; row < model->rowCount(); row++)
  5. {
  6. settings->beginGroup(QString("Row-%1").arg(row));
  7. for(int col = 0; col < model->columnCount(); col++)
  8. {
  9. settings->beginGroup(QString("Column-%1").arg(col));
  10. QModelIndex item = model->index(row, col);
  11. for(int role = 0; role < Qt::UserRole; role++)
  12. {
  13. model->setData(item, settings->value(QString("ItemDataRole-%1").arg(role))); // Restore from file
  14. }
  15. settings->endGroup();
  16. }
  17. settings->endGroup();
  18. }
  19. }
  20.  
  21.  
  22. void Victims::save()
  23. {
  24. QAbstractItemModel* model = ui->tw->model();
  25. for(int row = 0; row < model->rowCount(); row++)
  26. {
  27. settings->beginGroup(QString("Row-%1").arg(row));
  28. for(int col = 0; col < model->columnCount(); col++)
  29. {
  30. settings->beginGroup(QString("Column-%1").arg(col));
  31. QModelIndex item = model->index(row, col);
  32. for(int role = 0; role < Qt::UserRole; role++)
  33. {
  34. settings->setValue(QString("ItemDataRole-%1").arg(role), model->data(item, role)); // Save to file
  35. }
  36. settings->endGroup();
  37. }
  38. settings->endGroup();
  39. }
  40. }
To copy to clipboard, switch view to plain text mode 
Thanks for putting all this code.But the save slot doesn't work..after loading,the table is empty!!!!