[Qt-interest] Tree Item view Highlighter

Sajjad dosto.walla at gmail.com
Tue Jan 4 09:21:29 CET 2011


On Tue, Jan 4, 2011 at 8:32 AM, Andre Somers <andre at familiesomers.nl> wrote:

>  Op 4-1-2011 8:23, Sajjad schreef:
>
>
>
> On Tue, Jan 4, 2011 at 7:43 AM, Andre Somers <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:
>>
>> QVariant H3DHighlighterProxyModel::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...
>

I set the breakpoint at the slot

  void H3DNodeListWidget::highlightRegExpChanged()

{

    QRegExp regExp(m_nodeHighlightingEditor->text(),Qt::CaseInsensitive,QRegExp::FixedString);

     m_highlightModel->setFilterRegExp(regExp);

}


And i did not reach  the data() function while stepping into the code.



>
> 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.
>

I did not understand that part. A bit more elaboration might be helpful.



Regards
Sajjad


>
> André
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110104/2b2b5d85/attachment.html 


More information about the Qt-interest-old mailing list