Results 1 to 4 of 4

Thread: Filter columns in QAbstractItemModel

  1. #1
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    1
    Qt products
    Platforms
    MacOS X Unix/X11 Windows

    Question Filter columns in QAbstractItemModel

    Hi all readers,

    I've got a Model (a sub class of QAbstractTableModel) that work like a charm what ever the standard view I plug it in, that do not support the sort function.

    But this model as 'too' many columns most of the time... (thinks of it as the result of select * from table).

    So I build a simple model that filter columns, here is the code (in python) (btw I wanted to use QIdentityProxyModel but it does not exist in Qt 4.7...):
    Qt Code:
    1. class ModelColumnFilter(QtGui.QSortFilterProxyModel):
    2. def __init__(self):
    3. QtGui.QSortFilterProxyModel.__init__(self)
    4. self.colMapFromSource = {} # col source -> col proxy
    5. self.colMapFromProxy = tuple() # col proxy -> col source
    6.  
    7. def set_column_to_display(self, column_to_display):
    8. self.beginResetModel()
    9. self.colMapFromProxy = tuple(column_to_display)
    10. for i, x in enumerate(self.colMapFromProxy):
    11. self.colMapFromSource[x] = i
    12. self.endResetModel()
    13.  
    14. def flags(self, index):
    15. return self.sourceModel().flags(self.mapToSource(index))
    16.  
    17. def setSourceModel(self, sourceModel):
    18. QtGui.QSortFilterProxyModel.setSourceModel(self, sourceModel)
    19. # by default display all column with same order
    20. self.set_column_to_display(
    21. tuple(i for i in xrange(self.sourceModel().columnCount())))
    22.  
    23. def columnCount(self, parent=QtCore.QModelIndex()):
    24. return len(self.colMapFromProxy)
    25.  
    26. def mapFromSource(self, sourceIndex):
    27. if sourceIndex.isValid():
    28. if sourceIndex.column() in self.colMapFromSource:
    29. return self.createIndex(sourceIndex.row(),
    30. self.colMapFromSource[sourceIndex.column()])
    31. return QtCore.QModelIndex()
    32.  
    33. def mapToSource(self, proxyIndex):
    34. if proxyIndex.isValid():
    35. return self.sourceModel().index(proxyIndex.row(),
    36. self.colMapFromProxy[proxyIndex.column()])
    37. return QtCore.QModelIndex()
    To copy to clipboard, switch view to plain text mode 

    So, whatever the number of view I'm using to display data in this model, they see only what I need (more over in the order I need).

    This work great if I use it as is.

    But if I want to sort columns, and use an other QSortFilterProxyModel like this:
    Qt Code:
    1. mo = MyModel()
    2.  
    3. mf = ModelColumnFilter()
    4. mf.setSourceModel(mo)
    5. set_column_to_display([....])
    6.  
    7. mp.setSourceModel(mf)
    8.  
    9. view1 = QtGui.QTreeView()
    10. view1.setModel(mp)
    11. view1.setSortingEnabled(True)
    12.  
    13.  
    14. view2 = QtGui.QTreeView()
    15. view2.setModel(mp)
    16. view2.setSortingEnabled(True)
    To copy to clipboard, switch view to plain text mode 

    view2 cause the application to crash.
    I suppose it's a problem in my model (an index not very well formed ??).

    Tell me if I'm doing it wrong, or How can I change this to make it work.

    Thanks

  2. #2
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Filter columns in QAbstractItemModel

    Hi,

    I think the problem is that you try to connect two views to only one QSortFilterProxyModel you can try to create two sepatate QSortFilterProxyModel's for each view one

  3. #3
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Filter columns in QAbstractItemModel

    QSortFilterProxyModel only filter rows, if you need filter columns the easy way is rewrite the DB query to include ONLY the column you need.

    BTW: QIdentityProxyModel cant sort or filter the data in the model. QIdentityProxyModel is more suitable for transform the data of the source model. transforms like change num of second in a format "mm:ss" convert all the string to upper.
    Last edited by ecanela; 3rd August 2012 at 06:29. Reason: updated contents

  4. #4
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Filter columns in QAbstractItemModel

    another option is hide the columns in the view. but still is preferable rewrite your querys.

Similar Threads

  1. QFileDialog filter
    By wirasto in forum Newbie
    Replies: 12
    Last Post: 20th January 2010, 12:40
  2. Convolution filter
    By toutarrive in forum Qt Programming
    Replies: 5
    Last Post: 21st October 2009, 13:55
  3. QAbstractItemModel: size of columns
    By kima in forum Qt Programming
    Replies: 0
    Last Post: 24th October 2008, 09:16
  4. Replies: 8
    Last Post: 14th August 2008, 15:48
  5. Moving columns/hiding columns in QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 4
    Last Post: 14th March 2007, 16:22

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.