PDA

View Full Version : [SOLVED][PyQt4] QSortFilterProxyModel modify QStandardItemModel and QTreeView ??



grall
19th April 2013, 12:03
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 ?

Santosh Reddy
19th April 2013, 13:00
use "myItem = a.model().itemFromIndex(a)" for proxy model too, why do you differentiate?

grall
19th April 2013, 13:15
"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.

Santosh Reddy
19th April 2013, 13:39
Ok I missed the QStandardItemModel thing, any way you will need to map the index from the poxy model to the source model.



b = mapToSource(a);
myitem = a.model().sourceModel().itemfromIndex(b);


Edit: I mean change this to PyQt equivalent

grall
19th April 2013, 14:40
it works, thank you very much