Results 1 to 4 of 4

Thread: Model with two very different views

  1. #1
    Join Date
    Apr 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Model with two very different views

    I'm trying to imagine how to implement a rather unusual model/view situation. I'm trying to provide two distinct views of an XML hierarchy:

    1. A hierarchical tree view, using QTreeView.

    2. A list view, based on QStringListModel, which shows a list of the nodes which have xml:id attributes, along with some text from those nodes, displayed in a QListView.

    I can do each of these things separately -- in other words, I can implement a model based on QAstractItemModel, construct a tree in it, and display the tree in a QTreeView; and I can construct a model based on QStringListModel consisting of a list of the xml:ids, and display this in a QListView. But what I'd really like to do is have a single model which is hooked up to both a QTreeView and a QListView, so that any change to the model is simultaneously displayed in both controls.

    Is this possible? If so, how would you go about it?

    All help appreciated,
    Martin

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Model with two very different views

    Use a proxy model, either QSortFilterProxyModel (or derive a class from it) or make your own proxy model from scratch by subclassing QAbstractProxyModel.
    This proxy model is between a model and a view, so in your case it will filter out any other nodes than xml:id from your base XML model.
    In Qt docs you can found two examples demonstrating use of QSortFilterProxyModel.

    Edit:
    After taking a look on the QSortFilterProxyModel, I think that QSortFilterProxyModel::filterAcceptsRow() and QSortFilterProxyModel::filterAcceptsColumn() can be methods you want. They are virtual protected so you will need to subclass QSortFilterProxyModel, so take a look at Custom Sort/Filter Model example.
    Last edited by faldzip; 7th April 2010 at 07:41.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Apr 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Model with two very different views

    You're right, that's the way to go; but I'm already using a QSortFilterProxyModel to enable the user to filter the QStringListModel content based on regular expressions. Can you chain together multiple QSortFilterProxyModels (i.e., originalSource->QSortFilterProxyModel to produce item list->QSortFilterProxyModel to allow user filtering)?

    Cheers,
    Martin

  4. #4
    Join Date
    Apr 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Model with two very different views

    I've done this, but I'm encountering a problem. My core model is a DomModel object, taken directly from the SimpleDomModel example. That works well with the tree view. Then I'm using my own QSortFilterProxyModel descendant to filter the output, like this:

    bool HcmcXmlIdFilter::filterAcceptsRow ( int source_row, const QModelIndex &source_parent ) const{
    DomItem *parentItem;

    if (source_parent.isValid()){
    parentItem = static_cast<DomItem*>(source_parent.internalPointe r());
    DomItem *childItem = parentItem->child(source_row);
    if (childItem){
    QDomNode n = childItem->node();
    if (n.isElement()){
    if (n.toElement().hasAttribute("xml:id")){
    return true;
    }
    }
    }
    }
    return false;
    }

    However, no content is showing up in the QListView. Even when I set that function to return true by default, only the top level elements show up in the list; child elements in the tree do not seem to be processed. Any suggestions as to what I'm doing wrong?

    Cheers,
    Martin

Similar Threads

  1. how to disable views from a model
    By zeeeend in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2008, 21:14
  2. Question about a model with multiple views
    By awhite1159 in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2008, 00:27
  3. 2 Views 1 Model
    By PrimeCP in forum Qt Programming
    Replies: 3
    Last Post: 2nd October 2007, 01:40
  4. Model/Views Woes
    By Scorp1us in forum Qt Programming
    Replies: 3
    Last Post: 8th February 2007, 03:10
  5. Model Views and QLabel
    By naresh in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2006, 15:14

Tags for this Thread

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.