Results 1 to 9 of 9

Thread: How to view just a part of a model ?

  1. #1
    Join Date
    Jul 2006
    Location
    France
    Posts
    34
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to view just a part of a model ?

    Hye,

    I'm actually developping an application in PyQt, with Qt4.1 and python 2.4
    I have done my own QAbstractItemModel class (here 'M'), for managing my data (a tree like structure).
    I've got already a QTreeView (here called 'S') connected to this model and it work great.

    But, I would like that when a user click an element (or an index if you prefer here call 'I')of my tree:
    • display a new window (here is 'C')in my workspace (easy)
    • this window have a widget whitch is a treeview like item (just a treeview would be the best (I'm a bit lazy :P))
    • the model for the view should be 'M' (such as for 'S') BUT i do not want to display the parent (and siblings) of 'I', just his children


    I try to make a 'picture':

    Qt Code:
    1. The Tree in 'S':
    2. [A]
    3. |-[H]
    4. |-[I]
    5. | |-[K]
    6. | |-[L]
    7. ...
    8.  
    9. The quiet same tree in 'C':
    10. [I]
    11. |-[K]
    12. |-[L]
    13. ...
    To copy to clipboard, switch view to plain text mode 
    Does anyone know how can I do this ?
    Last edited by weepdoo; 6th June 2007 at 17:21. Reason: reformatted to look better

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to view just a part of a model ?

    Check out the QSortFilterProxyModel class. I think that might be what you're looking for.

    You subclass it and install an instance of it between your model and treeview. Whenever you need to, you simply change the state in your filter-object, so it will only pass on the subtree you want to see.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to view just a part of a model ?

    Set the rootIndex of the view to the index of the item whose children are to be visible.

  4. The following user says thank you to wysota for this useful post:

    weepdoo (11th June 2007)

  5. #4
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to view just a part of a model ?

    Well. Turns out it was even easier than I thought. Sorry for the misdirection.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  6. #5
    Join Date
    Jul 2006
    Location
    France
    Posts
    34
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to view just a part of a model ?

    Easyer than I though
    I will test it tomorow.

    Thanks to all of you.
    Thanks to TrollTech for this tremendous framework ^^

  7. #6
    Join Date
    Jul 2006
    Location
    France
    Posts
    34
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to view just a part of a model ?

    Hye everybody !

    I still have a problem with my proxy.
    I try to change my data root (as suggested earlier) so I redefine a new index method (to use my new data root) in my proxy as this (I repeat: it's python):
    Qt Code:
    1. class QtgProxyModel(QtGui.QSortFilterProxyModel):
    2. def __init__(self, model, newIndexRoot=None):
    3. super(QtGui.QSortFilterProxyModel, self).__init__()
    4.  
    5. #======= Attributes Declaration =========
    6. # We init the root of the proxy
    7. self._data = model.getNodeFromIndex(newIndexRoot) # the data root
    8. self._dObject = self._data.dDocObject # the dictonary of all children item
    9. #========================================
    10.  
    11. self.setSourceModel(model)
    12.  
    13. def index(self, row, column, parent):
    14. if not parent.isValid():
    15. parentNode = self._data
    16. else:
    17. parentNode = self._dObject[parent.internalId()]
    18.  
    19. childItem = self._dObject[id(parentNode.getChildFromNum(row))]
    20. if childItem is not None:
    21. return self.createIndex(row, column, id(childItem))
    22. else:
    23. return QtCore.QModelIndex()
    To copy to clipboard, switch view to plain text mode 


    But, when launch my application, it just stop. I'm running on window, and DrWatson give an unreadable report.

    After debugging, the only thing I know, is that the application blow away at line 21 at the "createIndex" call.

    Do I do the right thing ? If not, how should I do it (I read C++ too )?
    Is it a PyQt bug ?

    Thanks

  8. #7
    Join Date
    Jul 2006
    Location
    France
    Posts
    34
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to view just a part of a model ?

    Nobody know what I'm doing wrong ?

    Should I re implement the "index" method ?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to view just a part of a model ?

    You should set the root index in the view, not in the proxy. Don't touch the models, they are perfectly fine.

  10. The following user says thank you to wysota for this useful post:

    weepdoo (11th June 2007)

  11. #9
    Join Date
    Jul 2006
    Location
    France
    Posts
    34
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to view just a part of a model ?

    Thanks for your help.

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  3. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 13:01
  4. Replies: 6
    Last Post: 20th April 2006, 10:23
  5. Model, view, resize to show all data
    By jcr in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2006, 20:59

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
  •  
Qt is a trademark of The Qt Company.