Hello forum,
I have loaded a database into a tree view as follows:
nodetree.png
I have a textfield above the tree view where the user types the text and the matched tree item name with the entered text gets highlighted. There will be no changes(addition / deletion) within the tree view except the matched item will be highlighted.
If the matched item is within the uncollapsed tree item , it will be collapsed and shown highlighted.
I tried to get it done by subclassing the sor filter proxy model , but i could not make it functional. I need more help on this issue.
{
Q_OBJECT
public:
H3DHighlighterProxyModel
(QObject *parent
= 0);
~H3DHighlighterProxyModel();
protected:
private:
QVariant filterCheck
(const QModelIndex
&) const;
};
{
//get the handle to the underlyiing data - map the model index to the source model index
// QModelIndex sourceIndex = mapToSource(index);
// //make sure that the model index is valid
// if(!sourceIndex.isValid())
// {
// return QVariant();
// }
// if(role == Qt::BackgroundRole)
// {
// return filterCheck(sourceIndex);
// }
// else
// {
// return sourceIndex.data(role);
// }
if(role == Qt::BackgroundRole)
{
if(!filterRegExp().isEmpty())
{
if(text.contains(filterRegExp()))
{
}
}
}
}
class H3DHighlighterProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
H3DHighlighterProxyModel(QObject *parent = 0);
~H3DHighlighterProxyModel();
protected:
virtual QVariant data(const QModelIndex &index, int role) const;
private:
QVariant filterCheck(const QModelIndex&) const;
};
QVariant H3DHighlighterProxyModel::data(const QModelIndex &index, int role) const
{
//get the handle to the underlyiing data - map the model index to the source model index
// QModelIndex sourceIndex = mapToSource(index);
// //make sure that the model index is valid
// if(!sourceIndex.isValid())
// {
// return QVariant();
// }
// if(role == Qt::BackgroundRole)
// {
// return filterCheck(sourceIndex);
// }
// else
// {
// return sourceIndex.data(role);
// }
if(role == Qt::BackgroundRole)
{
QString text = QSortFilterProxyModel::data(index).toString();
if(!filterRegExp().isEmpty())
{
if(text.contains(filterRegExp()))
{
return QColor(Qt::lightGray);
}
}
}
return QSortFilterProxyModel::data(index,role);
}
To copy to clipboard, switch view to plain text mode
Any hint to sort it out?
Regards
Sajjad
Bookmarks