Filtering the QAbstractTableModel is slow.
-
Hello, I am using custom model that inherits QAbstractTableModel, QTableView, and custom class inherits QSortFilterProxyModel. I am using beginInsertRows and endInsertRows in my custom model.
When I apply filtering, GUI freeze for a short time. I override filterAcceptRows in my custom class. When I change the filter It freeze some time based on how many rows I have (+10.000). What can I do about it ?
Thanks a lot.
-
@ikuris said in Filtering the QAbstractTableModel is slow.:
What can I do about it ?
speed up your filterAcceptsRow() function would be a good start.
-
@ikuris said in Filtering the QAbstractTableModel is slow.:
Any advice on what I should look out for in my filterAcceptsRow() function?
Without code?
-
bool MyCustomClass::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
QModelIndex index_0 = sourceModel()->index(source_row,0,source_parent);
QModelIndex index_1 = sourceModel()->index(source_row,1,source_parent);
QModelIndex index_2 = sourceModel()->index(source_row,2,source_parent);
QModelIndex index_3 = sourceModel()->index(source_row,3,source_parent);bool flag1 = sourceModel()->data(index_0).toString().contains("filter1"); bool flag2 = sourceModel()->data(index_1).toString().contains("filter2"); // COMPARECOLUMN3&4 BASICALLY CHECKS FOR FLAGS (I HAVE IN MY CUSTOM CLASS THEN RETURNIN BOOLEAN bool flag3 = compareColum3(sourceModel()->data(index_2).toString().toUtf8().data()); bool flag4 = compareColumn4(sourceModel()->data(index_3).toString().toUtf8().data()); return flag1 && flag2 && flag3 && flag4;
}
-
@ikuris said in Filtering the QAbstractTableModel is slow.:
compareColum3
What does this function do ? Why does it need a plain char*
I would add a function to retrieve the whole data structure from the source model instead going through data()auto sm = static_cast<MyModel*>(sourceModel()); auto myStruct = sm->getCompleteData(source_row); bool flag1 = myStruct->whatever.contains(QLatin1String("filter1")); ...
This avoids a lot of to/fromQVariant conversions.
And you should do an early exit instead evaluating all 4 booleans. -
It is comparing strings based on the flags that I got. I got different flags for compareColumn3 and and compareColumn4 for their personal filtering settings. Basically functions makes able to find data between x and y, or applying other opperation != , <, >, >= ,<=).
Thanks for your suggestion I will try ASAP. Any other suggestions ?
-
@ikuris said in Filtering the QAbstractTableModel is slow.:
Any other suggestions ?
show compareColumn3() and 4 function.
-
bool MyCustomClass::compareColumn3(const std::string& filter)
{
switch(myCompareDataFlag)
{
case flagBigger: return filter < mFilter;
case flagSmalller: return filter > mFilter;
...
...
}
return true;}
Could converting QStriing (toUtf().data() ) be the main cause of slowing down my GUI? If It is I pass QString as parameter. For compareColumn4 I am parsing QString with regex to get usefull parts in that section.
-
Conversion is always slow, es. when you convert it three times for unknown reason.
Pass a const qSTring and make mFilter as QString too.