Hi Forum,

I have a database that has two tables representing a list of jobs and a list of tasks for each job.

My goal is to display this information in a QTreeView as well as in QTableView. Initially however, I want to display the QTreeView in two different ways.
1. Jobs with their tasks as children.
2. Three Task Categories with the tasks in those categories as children.

Currently I am constructing a QStandardItemModel like this;

Qt Code:
  1. QStringList jobs = api->jobs(); // Returns a QStringList of the database tables: e.g. job_id|JobTitle|task_id|TaskTitle
  2.  
  3. QStringList headerLabels;
  4. headerLabels << "Date" << "UUID" << "Assigned" << "Due Date" << "Status";
  5. m_stdmodel->setHorizontalHeaderLabels(headerLabels);
  6.  
  7. foreach(const QString& job, jobs) {
  8. QList <QStandardItem *> jobItem;
  9. jobItem << new QStandardItem(QIcon(":/png/64x64/accept.png"), api->jobTitle(job));
  10. QStringList tasks = api->tasksByJob(job);
  11. foreach(const QString& task, tasks) {
  12. QList<QStandardItem*> items;
  13. items << new QStandardItem(QIcon(":/png/64x64/remove.png"), api->taskTitle(task));
  14. items << new QStandardItem(task);
  15. items << new QStandardItem(api->taskAssignee(task));
  16. items << new QStandardItem(api->taskDue(task));
  17. items << new QStandardItem(api->taskStatus(task));
  18. //
  19. jobItem.at(0)->appendRow(items); // append child items to top-level item
  20. }
  21. m_stdmodel->appendRow(jobItem);
  22. }
  23. // Set the mode here so we can set different
  24. // models for different views.
  25. table.setModel(m_stdmodel);
  26. table.hideColumn(1);
  27. table.expandAll();
To copy to clipboard, switch view to plain text mode 

The problem with this is that I am technically viewing a copy of the data and not the data from the database itself. IMHO, this breaks the whole concept of using a model/view architecture.
It also means that any other views into the data are out of sync with each other.

My question is; what models should I be using and how do I keep the various views in sync?
I have decided I should be using a QAbstractProxyModel but I can't work out how to correctly implement the mapTo/FromSource so that the hierarchy is correct.

Thanks.