January 20, 2011

Ashwin Das Ashwin Das
Lab Rat
33 posts

Highlighting partial text inside QTableView

 

I have added a QLineEdit above my QTableView where when a user enters text, a filter happens on the tables contents using QSortfilterProxyModel
I am trying to highlight only the text inside cells of the QTableView which matched the text/reg exp in QLineEdit,
similar to the Qt Creator – Ctrl+F on the CPP Editor and it highlights only the matched strings.

I’m currently trying to do this using my own QStyledItemDelegate and doing inside the paint method, but I am finding it hard to do manage many things here in paint like Multiline, Background color on selection etc, is there a better way to do this?

Can I use QSyntaxHighlighter to highlight text in QTableView? or is there any other easier way?

Thanks.

14 replies

January 20, 2011

Gerolf Gerolf
Robot Herder
3287 posts

QSyntaxHighlighter works on a QTextDocument. If you create it on the fly in paint and use it for painting, it could work.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

January 21, 2011

Ashwin Das Ashwin Das
Lab Rat
33 posts

Thanks Gerolf, I will try doing that.

January 21, 2011

Andre Andre
Robot Herder
6420 posts

I have done it, and it works. You can use QTextDocument from a delegate to render rich text. However, it is not trivial to get it to work with all view types.

January 22, 2011

Ashwin Das Ashwin Das
Lab Rat
33 posts

I got the QTextDocument to work inside my Item Delegate,
but somehow the QSyntaxHighlighter doesnt work till I call a rehighlight on it.. like this ->

  1. QTextDocument *doc = new QTextDocument;
  2.     doc->setPlainText(text);
  3.     doc->adjustSize();
  4.  
  5.     FilterSyntaxHighlighter* highlighter = new FilterSyntaxHighlighter(regExp, doc);
  6.     highlighter->rehighlight();
  7.  
  8.     QAbstractTextDocumentLayout :: PaintContext context;
  9.  
  10.     doc->setPageSize(opt.rect.size());
  11.  
  12.     painter->setClipRect(opt.rect);
  13.     painter->translate( opt.rect.x(), opt.rect.y() );
  14.     doc->documentLayout()->draw(painter,context);

I guess I am missing some minor thing here.

January 25, 2011

badlogin badlogin
Lab Rat
1 posts

We’re (with Bobs [developer.qt.nokia.com]) using some internal code here for a practically same purpose. After a few discussions we published it (and a little sample application) on gitorious [gitorious.org].

modelviewutils.png

We’ve inherited subclass from QItemDelegate (it’s named MarkerItemDelegate) and used an extended QSortFilterProxyModel named GenericSortFilterModel to give a sorting regexp to the delegate. Note that MarkerItemDelegate is based on Qt Creator internal class with the same name, so you’re free to use it if you accept the terms of it’s LGPL license.

Also note that MarkerItemDelegate works incorrectly with a multi-line fields in a model when a selection breaks into a two lines.

January 25, 2011

Volker Volker
Ant Farmer
5428 posts

Be aware that the standard delegate on all item views is QStyledItemDelegate [doc.qt.nokia.com] as of Qt 4.4, not QItemDelegate [doc.qt.nokia.com]. See the API docs for the differences [doc.qt.nokia.com]

Althought this very likely to not solve your actual problem, it is strongly advised to use QStyledItemDelegate as your base class.

January 25, 2011

Bobs Bobs
Lab Rat
34 posts

Well, I agree that it’s not a quite perfect solution, but it have one definite advantage: it works! :)

Maybe we’ll polish and rewrite this component later, but at the moment I don’t really have a free time to do it. Just hope that this little sample will be useful for somebody (maybe a topicstarter) here.

 Signature 

Nokia Certified Qt Specialist.

January 25, 2011

Ashwin Das Ashwin Das
Lab Rat
33 posts

Thanks bobs and badlogin for sharing your example.
It is definitely useful.

I tried it and it works fine with few minor problems, I found with your example are the highlight doesnt move when row is resized, and on selection the cell doesnt show the highlight

Filter Example 1

I achieved my result using QTextDocument with QSyntaxHighlighter as previously mentioned, but I’m not sure if this it is an efficient one.
Anyways coding it was pretty simple, and it dint have the many issues like resizing etc worked fine, and with QSyntaxHighlighter it is easier to set various styles compared to painting everything ourselves. Preview ->

Filter Example 2

If you guys wanna have a look at it – let me know, I will share my code.

January 25, 2011

Ashwin Das Ashwin Das
Lab Rat
33 posts

Volker wrote:
Be aware that the standard delegate on all item views is QStyledItemDelegate [doc.qt.nokia.com] as of Qt 4.4, not QItemDelegate [doc.qt.nokia.com]. See the API docs for the differences [doc.qt.nokia.com]

Althought this very likely to not solve your actual problem, it is strongly advised to use QStyledItemDelegate as your base class.

Yes Volker I checked that too, my implementation also uses QItemDelegate now :(

I am new to C++ and Qt, and I started looking for similar solutions in Qt code, and found something similar to what I wanted in Qt Creator and achieved my result using QItemDelegate as they have used in Qt Creator’s – Search result window.
Access to functions like doLayout, QItemDelegate::drawFocus, QItemDelegate::drawBackground, etc., are easily available in QItemDelegate which I used in paint, and I could not find the alternates for this in QStyledItemDelegate. Will try to move my code to QStyledItemDelegate too.

January 25, 2011

Gerolf Gerolf
Robot Herder
3287 posts

Ashwin Das

perhaps you could make a wiki page for that? perhaps in developement, otherweis in code snippets…

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

January 25, 2011

Ashwin Das Ashwin Das
Lab Rat
33 posts

Gerolf wrote:
Ashwin Das

perhaps you could make a wiki page for that? perhaps in developement, otherweis in code snippets…

Sure will do,
But as of now, my example uses QItemDelegate and not QStyledItemDelegate.
As it is not recommended to use QItemDelegate, should I create the wiki now? or sometime later when I get it working with QStyledItemDelegate?

January 25, 2011

Gerolf Gerolf
Robot Herder
3287 posts

Both is possible, I would just try to replace QItemdelegate with QStyledItemdelegate and try what happens… :-)

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

February 9, 2011

zerg2011 zerg2011
Lab Rat
16 posts

Hi

I implemented highlighting using this way ( dynamically created QTextDocument ) for QListView .
Generally it works. But I have an issue with ItemDelegate::sizeHint () : if it returns a height below 20 then text in the lines disappear.

Could you please advise something ?

October 4, 2012

patylu001 patylu001
Lab Rat
1 posts

Hello I’m having the same problem.. I also used the code for QListView, but I keep getting an overlapped text..seems to be a problem with the height some lines disappear..
Did you solve your problem? Any hints?

 
  ‹‹ QPainter over Xlib window      signal slot question ››

You must log in to post a reply. Not a member yet? Register here!