[PyQt4] QSortFilterProxyModel doesn't work with my simple custom model
Hi,
I want to use the QSortFilterProxyModel on my custom model for sorting/filtering purpose. But when applying it, the list in my TableView is empty.
If I do not use my custom model, but the QStandardItemModel (as in the examples "basic sort/filter model"), it works.
So it seems my custom model is at fault here, although it works just fine if I do not use the QSortFilterProxyModel.
Does my custom model require additional method implementations for it to work?
Or do I have subclass the QSortFilterProxyModel? If so, what would I need to change?
Here is my custom model:
Code:
def __init__(self, datain, parent=None, *args):
self.arraydata = datain
def rowCount(self, parent):
return len(self.arraydata)
def columnCount(self, parent):
return len(self.arraydata[0])
def data(self, index, role):
if not index.isValid():
elif role != Qt.DisplayRole:
return QVariant(self.
arraydata[index.
row()][index.
column()])
Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model
How do you add data to your model? Do you remember about emitting signals?
Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model
In this simple programm I have an array
Code:
my_array = [['00','01','02'],
['10','11','12'],
['20','21','22']]
which will be given to the constructor of the model.
Code:
tablemodel = MyTableModel(my_array, self)
At this stage I simply want to display my model. Later on I will add the necessary methods where I can add items to the model. Or do I need these methods already for the sorting/filtering?
As for "emmiting signals" I am not sure what you are getting at. I know the idea behind the signal/slot thing, but I don't see where I need it in my model or view.
Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model
Can you show us the code where you use the sort filter proxy model?
Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model
here it is:
Code:
myModel = MyTableModel(my_array)
proxyModel.setSourceModel(myModel)
#proxyModel.setDynamicSortFilter(True)
myView.setModel(proxyModel)
Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model
The code looks fine, could you check what does columnCount() return for both the source model and the proxy?
Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model
I added the print statement, so I can see what will be returned.
Code:
def columnCount(self, parent):
print(len(self.arraydata[0]))
return len(self.arraydata[0])
as well for
Code:
myView.setModel(proxyModel)
as for
Code:
myView.setModel(myModel)
it always returns 3
Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model
It's not what I mean. Call columnCount() on both the proxy and the source model and print the result. It is important to see what the proxy reports (0 or 3).
By the way, if you omit the proxy then the view displays the data properly, correct?
Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model
Code:
print("proxyModel:")
print("myModel:")
results in:
Code:
proxyModel:
3
myModel:
3
Quote:
By the way, if you omit the proxy then the view displays the data properly, correct?
yes. It works fine without the proxy model.
Ps: I will be out for a few hours (so my next reply won't be coming right away :D).
And before I forget it: thanks for the fast replies.
Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model
The only thing that comes to my mind is that you didn't implement the parent() method although it might already be implemented for the table model. On the other hand it wouldn't hurt to return an empty model index from it. Also make sure you return non-zero values of rowCount and columnCount only if the parent index is invalid.
Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model
Would anyone have an example where QSortFilterProxyModel is used with a custom model?
Preferably for PyQt.
Re: [PyQt4] QSortFilterProxyModel doesn't work with my simple custom model
I do (in c++) :) But your code really looks fine apart from ignoring the parent indexes.
Here is a complete working example:
Code:
#include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include <QTableView>
#include <QApplication>
public:
for(int i=0;i<3;i++){
QVector<int> row;
for(int j=0;j<3;j++)
row << qrand()%100;
m_data << row;
}
}
return m_data.size();
}
return m_data.at(0).size();
}
else if(role
!=Qt
::DisplayRole) return QVariant();
return m_data[index.row()][index.column()];
}
private:
QVector< QVector<int> > m_data;
};
int main(int argc, char **argv){
Model model;
proxy.setSourceModel(&model);
tv.setModel(&proxy);
tv.show();
return app.exec();
}
Check if the problem is not outside the model class (i.e. maybe the proxy model gets deleted somewhere inbetween setting it on the view and launching the application).