Results 1 to 2 of 2

Thread: linking disparate models for same non-qt based data object

  1. #1
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default linking disparate models for same non-qt based data object

    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

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: linking disparate models for same non-qt based data object

    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
    Qt Code:
    1. struct TreeNode
    2. {
    3. TreeNode * parent;
    4. QVector< TreeNode * > children;
    5. MyModelElement * pointerToModelForThisNode;
    6. }
    To copy to clipboard, switch view to plain text mode 

    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.

  3. The following user says thank you to d_stranz for this useful post:

    winkle98 (4th November 2011)

Similar Threads

  1. Replies: 9
    Last Post: 11th April 2011, 10:05
  2. Shared Object In Qt and Linking to main application
    By Ashutosh2k1 in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2011, 10:36
  3. Replies: 8
    Last Post: 18th December 2010, 15:07
  4. Replies: 4
    Last Post: 28th August 2010, 13:17
  5. Proxy models and slection models
    By amunitz in forum The Model-View Framework
    Replies: 1
    Last Post: 23rd September 2008, 14:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.