[Qt-interest] Tree Item view Highlighter

Andre Somers andre at familiesomers.nl
Tue Jan 4 07:43:51 CET 2011


Op 3-1-2011 23:29, Sajjad schreef:
> Hello forum,
>
>
> The following topic is the follow-up which has been discussed several 
> days ago.
It would have been better to continue the discussion in that tread then.
> I want to highlight a particular tree item based on pattern matching. 
> In  that case i have sub-classed the QSortFilterProxyModel and 
> over-ridden the following function
>
>
> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> bool  H3DHighlighterProxyModel::filterAcceptsRow(int  source_row,  const  QModelIndex  &source_parent)  const
> {
>      std::cout  <<  "In  the  filtration"  <<  std::endl;
>      QColor  highLighter  =  Qt::cyan;
>      QColor  noHighLighter  =  Qt::white;
>      if(sourceModel()->data(source_parent).toString().contains(filterRegExp()))
>      {
>          std::cout  <<  "Will  be  highlighted"  <<  std::endl;
>          sourceModel()->setData(source_parent,QVariant(highLighter),Qt::BackgroundRole);
>      }
>      else
>      {
>          std::cout  <<  "Will  not  be  highlighted"  <<  std::endl;
>          sourceModel()->setData(source_parent,QVariant(noHighLighter),Qt::BackgroundRole);
>      }
>      return  true;
> }
>
>
>
>
> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
>
> But i am not getting any high-lighting effect with the textChanged 
> signal in the line edit . The appropriate slot has been connected as 
> follows:
>
>
>
> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
> void  H3DNodeListWidget::highlightRegExpChanged()
> {
>      QRegExp  regExp(m_nodeHighlightingEditor->text(),Qt::CaseInsensitive);
>      m_highlightModel->setFilterRegExp(regExp);
> }
>
>
>
> ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
>
> If there is anything missing in the process or logical mistake, please 
> mark it out for me that i am missing.
>
Yes. Don't reimplement filterAcceptsRow, just let it return true (it 
does that by default, I think). Instead, reimplement the data() 
function, and _return_ the color you want for the rows that you want to 
highlight when the background role is queried. Otherwise, return 
whatever data the source model provides you. Something like this:

QVariantH3DHighlighterProxyModel::data(const  QModelIndex  &index, int  role)  const

{

	QModelIndex sourceIndex = mapToSource(index); //get a handle on the underlying data
	if (!sourceIndex.isValid())
		return QVariant();

	if (role == Qt::Background) {
		if (//needs to be highlighted) {
			return Qt::cyan;
		} else {
			return QVariant(); //use the default background color, whatever that may be
		}
	} else {
		return sourceIndex.data(role);
	}
}


André

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110104/52764a3e/attachment.html 


More information about the Qt-interest-old mailing list