The use case is that i am populating the listView with the filtererd rows.
Here is the code.
{
Q_OBJECT
public:
ModelFilterProxyModel
(QObject *parent
= 0);
protected:
virtual bool filterAcceptsRow
(int sourceRow,
const QModelIndex &sourceParent
) const;
private:
};
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;
};
To copy to clipboard, switch view to plain text mode
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
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);
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);
To copy to clipboard, switch view to plain text mode
Bookmarks