PDA

View Full Version : linking disparate models for same non-qt based data object



winkle98
3rd November 2011, 03:53
Hi,

I've got a pretty complex model/view situation I'm dealing with. I'm hoping you guys can offer some advice.

I have created a model based on QAbstractItemModel, that retrieves data from our non-qt based data object directly to update the information four the QTableView that is the main focus widget for the application. Basically, I arbitrarily determined what a row index means and what a column index means and I can use that information to look up values in my data object. It works pretty smoothly for the QTableView. However, I now need to add a QTreeView to the mix. (Actually 2 and they're each looking at different information. the row/column designations don't have the same meaning in the QTreeView. (eg row = roomNumber, column=student name, in the table. The tree is looking at course name and book list...just as an example)

I would like to take my main data model and use it with QTreeView. I haven't been able to figure out hot to get this to work yet, because of the disparate information. Using a proxy model and setting up the mapping methods didn't work because the information I'm looking for doesn't alway directly map. For example, since the base model was generated with a QTableView in mind the modelindexes have no idea about the parenting required to work with a QTreeView.

Has anyone faced this problem. Have you seen any good ideas on approaches? I've been staring and staring and the best idea I've been able to come up with is to use multiple data models, but chaining together the datachanges gets klunky.

I apologize if this isn't clear. I'm happy to attempt to clarify.

thank you

d_stranz
3rd November 2011, 22:17
The few times I have dealt with this problem, I have beat my head against the wall for a few days trying to figure out the parent-child relationships, and have ended up implementing an internal tree data structure which maps the external model into the tree needed by QTreeView. The tree node corresponding to the row/column in the view is stored as a pointer in the QModelIndex created in the index() method.

Nodes in the tree can be as simple as


struct TreeNode
{
TreeNode * parent;
QVector< TreeNode * > children;
MyModelElement * pointerToModelForThisNode;
}


and your QAbstractItemModel simply stores a pointer to the root of the tree, then traverses it as appropriate to create QModelIndex values for parent and child relationships in the QTreeView.