Results 1 to 5 of 5

Thread: [SOLVED][PyQt4] QSortFilterProxyModel modify QStandardItemModel and QTreeView ??

  1. #1
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default [SOLVED][PyQt4] QSortFilterProxyModel modify QStandardItemModel and QTreeView ??

    Hello everyone,

    I found something very curious about QSortFilterProxyModel and I have trouble dealing with this :
    Here is the situation :

    I have a QTreeView with his model QStandardItemModel.
    My QStandardItemModel contains some Customized QStandardItem.
    self.listlabelGroupView = QTreeView(self.widget)
    self.listlabelGroupView.setExpandsOnDoubleClick(Fa lse)
    self.listlabelGroupView.setSelectionBehavior(QAbst ractItemView.SelectRows)
    self.listlabelGroupView.setDragEnabled(True)
    self.listlabelGroupView.setRootIsDecorated(False)
    self.listlabelGroupView.setSortingEnabled(True)
    self.listlabelGroupView.setFont(font)
    self.listlabelGroupView.sortByColumn(1, Qt.AscendingOrder)
    self.listlabelGroupModel = QStandardItemModel(0,1)
    self.listlabelGroupModel.setHorizontalHeaderLabels (["Name"])
    self.filter = QSortFilterProxyModel()
    self.filter.setFilterKeyColumn(0)
    self.filter.setSourceModel(self.listlabelGroupMode l)
    self.filter.setDynamicSortFilter(True)
    self.listlabelGroupView.setModel(self.filter)
    I want to get my Customized QStandardItem when I double click on the QTreeView with this code :
    a = self.listlabelGroupView.selectionModel().currentIn dex()
    myItem = a.model().sourceModel().itemFromIndex(a)
    but it does not work, myItem has NoneType and I don't know why because when I don't use any filter, with the same code it works !
    self.listlabelGroupView.setModel(self.listlabelGro upModel)

    a = self.listlabelGroupView.selectionModel().currentIn dex()
    myItem = a.model().itemFromIndex(a)
    # myItem has type : CustomQStandardItem which is normal
    I have also checked that the source model of the filter and my QStandardItemModel are the same object and it is only when I set the filter to my QTreeView that the QStandardItemModel "bugs".

    Does anyone have any explanations to this behaviour ?
    Last edited by grall; 19th April 2013 at 13:40.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: [PyQt4] QSortFilterProxyModel modify QStandardItemModel and QTreeView ??

    use "myItem = a.model().itemFromIndex(a)" for proxy model too, why do you differentiate?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [PyQt4] QSortFilterProxyModel modify QStandardItemModel and QTreeView ??

    "myItem = a.model().itemFromIndex(a)" will not work because a.model() returns a QSortFilterProxyModel and the function itemFromIndex() is a function of QStandardItemModel class and therefore cannot be called by a QSortFilterProxyModel object.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: [PyQt4] QSortFilterProxyModel modify QStandardItemModel and QTreeView ??

    Ok I missed the QStandardItemModel thing, any way you will need to map the index from the poxy model to the source model.

    Qt Code:
    1. b = mapToSource(a);
    2. myitem = a.model().sourceModel().itemfromIndex(b);
    To copy to clipboard, switch view to plain text mode 

    Edit: I mean change this to PyQt equivalent
    Last edited by Santosh Reddy; 19th April 2013 at 12:46.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    grall (19th April 2013)

  6. #5
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [PyQt4] QSortFilterProxyModel modify QStandardItemModel and QTreeView ??

    it works, thank you very much

Similar Threads

  1. QTreeView with QStandardItemModel
    By aekilic in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2011, 09:11
  2. Replies: 12
    Last Post: 5th July 2009, 16:03
  3. QTreeView with QStandardItemModel
    By steg90 in forum Newbie
    Replies: 3
    Last Post: 16th May 2007, 09:28
  4. Modify model data in QTreeView
    By YuriyRusinov in forum Qt Programming
    Replies: 6
    Last Post: 26th October 2006, 17:28
  5. can't do a QTreeView with a QStandardItemModel
    By Nuria in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2006, 00:41

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.