PDA

View Full Version : filterAcceptRows() is being called many times for same souceRow.



kaushal_gaurav
18th February 2009, 13:01
Hi,

I have written my model driven from QSortFilterProxyModel
and i have implemented the function


virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;

but my function is being called repeatedly for same sourceRow, resulting in performance hit.

Am i missing something?

Please help me...
Regards,
Gk

wysota
19th February 2009, 01:11
It depends what you do with the model data. If you gave us more info, maybe we could pinpoint the problem. As a solution without knowing your usecase I suggest you cache the result of calculating the return value of filterAcceptsRow so that you don't have to recalculate it all over. This should reduce the impact of what you observe.

kaushal_gaurav
19th February 2009, 03:49
The use case is that i am populating the listView with the filtererd rows.
Here is the code.



class ModelFilterProxyModel : public QSortFilterProxyModel
{
Q_OBJECT

public:
ModelFilterProxyModel(QObject *parent = 0);
void SetSearchStr(QString str);

protected:
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;

private:
QString m_ManufSearchStr;
};





bool ModelFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
bool status = false;
bool found = false;
QModelIndex indexMod = sourceModel()->index(sourceRow, DevicesModel::MODEL, sourceParent);
QString modelStr = sourceModel()->data(indexMod).toString();

QModelIndex index = sourceModel()->index(sourceRow, DevicesModel::MANUFACTURER, sourceParent);
QString manufacturetStr = sourceModel()->data(index).toString();
bool found = false;
// Checking for models by the specific manufacturer and eleminating duplicate models.
for (int row = sourceRow; row >= 0 ; --row)
{
QModelIndex index1 = sourceModel()->index(row - 1, DevicesModel::MODEL, sourceParent);
QModelIndex indexManf1 = sourceModel()->index(row - 1, DevicesModel::MANUFACTURER, sourceParent);
QString modelStr1 = sourceModel()->data(index1).toString();
QString manufacturetStr1 = sourceModel()->data(indexManf1).toString();
if (modelStr == modelStr1 && manufacturetStr == manufacturetStr1)
{
found = true;
// No need to search further - so break.
break;
}
}
if (!found && manufacturetStr == m_pPrivate->m_ManufSearchStr)
{
status = true;
}
return status;
}




m_ModelProxyModel->setDynamicSortFilter(true);
m_ModelProxyModel->setSourceModel(m_Model);
m_ModelProxyModel->sort(DevicesModel::MODEL);
m_Ui.modelListView->setModel(m_ModelProxyModel);
m_Ui.modelListView->setModelColumn(DevicesModel::MODEL);