PDA

View Full Version : QTreeView & Sql Database.



gcain
18th October 2013, 01:05
Is the only way to display a QTreeView to loop through a record set and manually construct a hierarchical model?

So far the only examples I can find for QTreeView are pretty much manually constructed. What if I wanted changes to the data to be displayed in other widgets or written back to the data source?

All I can find are examples like this:


QStringList jobslist = jobs();
m_jobsmodel = new QStandardItemModel;

QStringList headerLabels;
headerLabels << "Date" << "UUID" << "Assigned" << "Due Date" << "Status";
m_jobsmodel->setHorizontalHeaderLabels(headerLabels);

foreach(const QString& job, jobslist) {
QList <QStandardItem *> jobItem;
jobItem << new QStandardItem(QIcon(":/png/64x64/accept.png"), jobTitle(job));

QStringList tasks = tasksByJob(job);
foreach(const QString& task, tasks) {
QList<QStandardItem*> items;
items << new QStandardItem(QIcon(":/png/64x64/remove.png"), taskTitle(task));
items << new QStandardItem(task);
items << new QStandardItem(taskAssignee(task));
items << new QStandardItem(taskDue(task));
items << new QStandardItem(taskStatus(task));

jobItem.at(0)->appendRow(items); // append child items to top-level item
}
m_jobsmodel->appendRow(jobItem);
}


I am aware that data in an sql table is 2D, but surely there is a mechanism to display 2D data in a hierarchical form without having to construct a copy of what is in the database?

Thanks for your advice in advance.

pkj
18th October 2013, 05:56
There are QSqlTableModel , QSqlRelationalTableModel and other related classes. However, they follow more of a master detail concept than a tree like structure.