[Qt-interest] Tree Item view Highlighter

Andre Somers andre at familiesomers.nl
Tue Jan 4 08:32:51 CET 2011


Op 4-1-2011 8:23, Sajjad schreef:
>
>
> On Tue, Jan 4, 2011 at 7:43 AM, Andre Somers <andre at familiesomers.nl 
> <mailto:andre at familiesomers.nl>> wrote:
>
>     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.
>
>
> Sorry for the discontinuity.
>
>
>>     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);
>     	}
>     }
>
>
> According to the suggestion the id as follows:
>
> ///////////////////////////////////////////////////////////////////////////////////////////////////////
> QVariant  H3DHighlighterProxyModel::data(const  QModelIndex  &index,  int  role)  const
> {
>      //get   the  handle  to   the  underlyiing  data
>      QModelIndex   sourceIndex  =  mapToSource(index);
>      if(!sourceIndex.isValid())
>      {
>          return  QVariant();
>      }
>      if(role  ==  Qt::BackgroundRole)
>      {
>          if(sourceModel()->data(sourceIndex).toString().contains(filterRegExp()))
>          {
>              return  Qt::cyan;
>          }
>          else
>          {
>              //return  the  default  background  color
>              //whatever  may  it  be
>              return  QVariant();
>          }
>      }
>      else
>      {
>          return  sourceIndex.data(role);
>      }
> }
>
>
>
> //////////////////////////////////////////////////////////////////////////////////////////////////////// 
>
>
> Is there anything missing up there? I removed the other function that 
> i over-rid mentioned in the previous post.
>
> I am still not getting any effect. The textchanged() signal is 
> connected to  the slot as before.
Ah, of course... Sorry about that.

First of all: are you getting an effect if you set a fixed regexp to 
begin with? If not, then something is wrong in the code above. Set a 
breakpoint where you return the color and see if you ever arrive there 
at all...

However, I think the problem is, that you need to notify your view that 
the data has changed if your regexp changes. For testing, just emit 
reset(), but in your real implementation, you should probably be a bit 
nicer and emit dataChanged() with the right modelindexes.

André

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


More information about the Qt-interest-old mailing list