PDA

View Full Version : filter tree item by highlight



sajis997
8th February 2012, 16:45
Hello forum,

I have loaded a database into a tree view as follows:

7382


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.





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);
}






Any hint to sort it out?


Regards
Sajjad

sajis997
6th March 2012, 13:13
Hello forum,


In the manual it says that , "In addition to sorting, QSortFilterProxyModel can be used to hide items that do not match a certain filter." I would rather like to highlight the item that matches and i do not want to hide any thing in my tree model.


Please take a look at the attached image

http://www.student.itn.liu.se/~sajis997/nodetree.png

I have loaded a node database into the tree view. To increase the usability i also have a line edit so that the user finds it easier to scroll to a particular tree item once the item entered in the line edit matches any of the item inside the tree view.

I would have the following functionalities:

1. Matched tree item gets high-lighted.
2. If the item resides inside the un-expanded parent item, it will be highlighted after it has been expanded.

I got to look at the QSortFilterProxyModel and but the related examples shows how the the items filtered with the regular expression and the table views is also altered to reflect the filtered items. The altercation results is removing item from the views, which i do not want to have. I just want to get the tree item high-lighted.

I hope to get some tips to implement this mechanism into my project.

If i have not explained well enough please mention it. I shall elaborate again



Regards
Sajjad

d_stranz
6th March 2012, 17:07
So if you do not want to filter out any items from the tree, then using a QSortFilterProxyModel is probably overkill for this problem. However, if you do use it, then you need to implement the QSortFilterProxyModel::data() method to handle the case where the "role" parameter has the values Qt::ForegroundRole and Qt::BackgroundRole. If the modelIndex is one that should be highlighted, then you should return a QBrush that is your highlight color for the text and background, respectively. If it is not a highlighted item, then you simply return QVariant() and the model/view architecture will display it normally.

If you only have one tree displaying the model (or if you want all views of the model to show the same highlight), then you don't actually need a proxy, you can simply handle this in the model itself.

As you have seen from the examples, QSortFilterProxyModel is generally used for much more complex filtering of a model.

sajis997
7th March 2012, 03:15
Thanks for the hint. Lets not over kill it. I already have the sub-classed Model from QAbstractItemModel. And i have over-ridden the data(...) as follows:



QVariant H3DTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();

H3DTreeItem *item = static_cast<H3DTreeItem*>(index.internalPointer());

if(role == Qt::DecorationRole)
{
return QIcon(":/H3D/images/Node.png");
}

/*
display role is used to access string that
can be displayed as text in the view
*/
else if(role == Qt::DisplayRole)
{
return item->data(index.column());
}
else if(role == Qt::UserRole)
{
return QPixmap(":/H3D/images/Node.png");
}
else if(role == Qt::ToolTipRole)
{
//return item->data(index.column());
if(m_nodeFactory)
{
return m_nodeFactory->getNodeData(item->data(index.column()).toString())->nodeData();
}
else
return item->data(index.column());
}
// else if(role == Qt::BackgroundRole)
// {
// return item->data(index.column());
// }
// else if(role == Qt::BackgroundColorRole)
// {
// return item->data(index.column());
// }
// else if(role == Qt::ForegroundRole)
// {
// return item->data(index.column());
// }
else
return QVariant();
}



What other changes i have to do over this model subclasses so that the entered line edit string pattern will match the item in the tree-views and high-light it or expand and high-light it.



Regards
Sajjad

d_stranz
7th March 2012, 03:42
I would look at reimplementing the QAbstractItemModel::match() method so that it searches through your tree to return the list of indexes that match the search string. You would then call beginResetModel(), save this list to a member variable in your model class, and trigger a refresh of the view by calling endResetModel().

In the data() method, if the role is Qt::BackgroundRole or Qt::ForegroundRole -and- the index is on the list you saved, you return appropriate brushes for background and foreground highlight (as QVariant), otherwise you return an empty QVariant to show the normal (non-highlighted) appearance.

Note that Qt::BackgroundColorRole is deprecated; you should eliminate this else clause and simply look at the BackgroundRole.

Edit: To expand the highlighted nodes, I think you need to implement a handler for the model's reset() signal in the widget that holds your QTreeView. When you receive it, retrieve the list of matched indexes from the model, then iterate over it, calling the QTreeView::expand() slot for each one.

Further edit, and maybe a simpler way to do this: If the default appearance of selected items is OK for you, then you don't need to implement the BackgroundColor and ForegroundColor role handlers. Instead, pass the results of the match() method to a QItemSelectionModel subclass and set this on the view. The view will then display the selected items with the right colors. This way, you could have the same model displayed in different trees, each one with a different set of nodes highlighted. (Doing it all within the model itself means that every view would be the same).