gcain
16th October 2013, 00:44
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;
QStringList jobs = api->jobs(); // Returns a QStringList of the database tables: e.g. job_id|JobTitle|task_id|TaskTitle
QStringList headerLabels;
headerLabels << "Date" << "UUID" << "Assigned" << "Due Date" << "Status";
m_stdmodel->setHorizontalHeaderLabels(headerLabels);
foreach(const QString& job, jobs) {
QList <QStandardItem *> jobItem;
jobItem << new QStandardItem(QIcon(":/png/64x64/accept.png"), api->jobTitle(job));
QStringList tasks = api->tasksByJob(job);
foreach(const QString& task, tasks) {
QList<QStandardItem*> items;
items << new QStandardItem(QIcon(":/png/64x64/remove.png"), api->taskTitle(task));
items << new QStandardItem(task);
items << new QStandardItem(api->taskAssignee(task));
items << new QStandardItem(api->taskDue(task));
items << new QStandardItem(api->taskStatus(task));
//
jobItem.at(0)->appendRow(items); // append child items to top-level item
}
m_stdmodel->appendRow(jobItem);
}
// Set the mode here so we can set different
// models for different views.
table.setModel(m_stdmodel);
table.hideColumn(1);
table.expandAll();
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.
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;
QStringList jobs = api->jobs(); // Returns a QStringList of the database tables: e.g. job_id|JobTitle|task_id|TaskTitle
QStringList headerLabels;
headerLabels << "Date" << "UUID" << "Assigned" << "Due Date" << "Status";
m_stdmodel->setHorizontalHeaderLabels(headerLabels);
foreach(const QString& job, jobs) {
QList <QStandardItem *> jobItem;
jobItem << new QStandardItem(QIcon(":/png/64x64/accept.png"), api->jobTitle(job));
QStringList tasks = api->tasksByJob(job);
foreach(const QString& task, tasks) {
QList<QStandardItem*> items;
items << new QStandardItem(QIcon(":/png/64x64/remove.png"), api->taskTitle(task));
items << new QStandardItem(task);
items << new QStandardItem(api->taskAssignee(task));
items << new QStandardItem(api->taskDue(task));
items << new QStandardItem(api->taskStatus(task));
//
jobItem.at(0)->appendRow(items); // append child items to top-level item
}
m_stdmodel->appendRow(jobItem);
}
// Set the mode here so we can set different
// models for different views.
table.setModel(m_stdmodel);
table.hideColumn(1);
table.expandAll();
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.