Results 1 to 5 of 5

Thread: Displaying a model as a QTreeView & Table.

  1. #1
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Displaying a model as a QTreeView & Table.

    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.

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a model as a QTreeView & Table.

    Use QSortFilterProxyModel ... mapTo/FromSource have been implemented in it.
    To keep data in sync, you need a single source of data on which both models work upon.

  3. #3
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Displaying a model as a QTreeView & Table.

    Quote Originally Posted by pkj View Post
    Use QSortFilterProxyModel ... mapTo/FromSource have been implemented in it.
    Thanks.
    To keep data in sync, you need a single source of data on which both models work upon.
    Would I have a single model that the other two models proxy data from?

  4. #4
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displaying a model as a QTreeView & Table.

    If you can describe a datastructure with a single tree then go for a single model. It will solve all your problems of syncing. And is a very neat solution too.
    But with your problem, I cannot see a single tree datastructure modelling your problem. If you cannot have a single tree, then there is no other way than to have two independent models, looking at the two trees, and a Mediator like class managing the sync between the two data structures. The models will have made public their begininsertrows etc, which the mediator uses to insert rows in the two trees and informs the other model about the changes.

  5. #5
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Displaying a model as a QTreeView & Table.

    Quote Originally Posted by pkj View Post
    If you can describe a datastructure with a single tree then go for a single model. It will solve all your problems of syncing. And is a very neat solution too.
    But with your problem, I cannot see a single tree datastructure modelling your problem. If you cannot have a single tree, then there is no other way than to have two independent models, looking at the two trees, and a Mediator like class managing the sync between the two data structures. The models will have made public their begininsertrows etc, which the mediator uses to insert rows in the two trees and informs the other model about the changes.
    Are you able to link me to an example or some documentation on how the mediator class would be structured? I am struggling to wrap my head around it.

    I am guessing you would just have opposing signals and slots updating the other model on change.
    This solution does seem quite simple really.

    Maybe I'll go write a simple implementation of what I want and see how it works actually...

    Thanks.

Similar Threads

  1. Replies: 0
    Last Post: 2nd November 2011, 11:13
  2. Displaying Image's Color Table
    By ramstormrage in forum Newbie
    Replies: 3
    Last Post: 13th May 2008, 17:07
  3. Table Model / View Problem -- Data Not Displaying
    By jhendersen in forum Qt Programming
    Replies: 1
    Last Post: 22nd April 2007, 06:45
  4. Displaying QAbstractItemModel in a Table
    By bmesing in forum Qt Programming
    Replies: 1
    Last Post: 28th March 2007, 11:11
  5. displaying any table on a qdatatable
    By Philip_Anselmo in forum Newbie
    Replies: 4
    Last Post: 9th May 2006, 22:12

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.