Re: Highlight Tree Item Viw
Quote:
Originally Posted by sajis997
5. Set view to both the model and proxy model.
You should be setting model to both the view and proxy model (may be just a typo)
Quote:
Originally Posted by sajis997
const QModelIndex topLeft = m_highlightModel->index(0,0,QModelIndex());
const QModelIndex bottomRight = m_highlightModel->index(0,m_highlightModel->columnCount()-1,topLeft);
This will not work, as you are just cheking the first row, rest of the rows are ignored
You should be running the search on all the items recessively something like this
Code:
// Check for the string, including all children
void filterCheck
(const QModelIndex & parent
) //Pass root index here, it should take care of rest {
if(parent.isValid())
{
if(!parent.data().toString().isNull())
{
bool value = false;
if(LineEdit filter is matching) // Check filter
value = true;
emit highlightItem(parent, value);
}
int rowCount = parent.model()->rowCount(parent);
int colCount = parent.model()->columnCount(parent);
for(int i = 0; i < rowCount; i++)
for(int j = 0; j < colCount; j++)
filterCheck(parent.model()->index(i, j, parent)); //recursive call
}
}
Re: Highlight Tree Item Viw
Hello Santosh,
I have tried to follow the suggestion as follows:
Code:
{
if(!filterRegExp().isEmpty())
{
if(sourceModel()->data(parent).toString().contains(filterRegExp()))
{
return Qt::yellow;
}
else
{
int rowCount = parent.model()->rowCount(parent);
int columnCount = parent.model()->columnCount(parent);
for(int i = 0; i < rowCount; i++)
for(int j = 0; j < columnCount; j++)
filterCheck(parent.model()->index(i,j,parent));
}
}
else
{
//return the default background color
//whatever may it be
}
}
{
//get the handle to the underlyiing data - map the model index to the source model index
//make sure that the model index is valid
if(!sourceIndex.isValid())
{
}
if(role == Qt::BackgroundRole)
{
return filterCheck(sourceIndex);
}
else
{
return sourceIndex.data(role);
}
}
But i am getting the same effect as before. Do i have to check for like if the the tree-view is expanded or not, because the child item is not highlighted.
And again,i have sub-classed the QSortFilterProxyModel and i have over-ridden the data() function as you can see in the above code snippet. I am getting the highlighting effect as before on the parent item and not to the child items . The items that are not matched are removed from the tree view. I do not want the tree view to change at all. I just want the matched item to be highlighted and the non-matched item to remain same as it was in the tree-view.
Any more hint on the issue?
Regards
Sajjad
Re: Highlight Tree Item Viw
Ok, try this, sub-class QSortFilterProxyModel, and re-implement only data(), like this
Code:
{
if(role == Qt::BackgroundRole)
{
if(text.contains(hightLight) and !hightLight.isEmpty()) // QString hightLight; //match string, replace this with QRegExp if required.
return QColor(Qt
::lightGray);
//hightlight color }
}
this is simple, and will work
Re: Highlight Tree Item Viw
Hello Santosh,
I am getting the old effect as before. Let me elaborate again , i must be missing something here.
In one of the previous posts you have mentioned to Set view to both the model and proxy model. Does it mean something as follows:
treeView->setModel(sourceModel);
treeView->setModel(proxyModel);
I do not understand the point of doing above. Instead should not it be something as follows:
Code:
highlightModel = new highlightModel(this); // subclass of the QSortFilterProxyModel
highlightModel->setSourceModel(sourceModel);
treeView->setModel(highlightModel);
Regards
Sajjad