Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
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?
QSyntaxHighlighter works on a QTextDocument. If you create it on the fly in paint and use it for painting, it could work.
Thanks Gerolf, I will try doing that.
andre last edited by
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.
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 ->
@QTextDocument *doc = new QTextDocument;
FilterSyntaxHighlighter* highlighter = new FilterSyntaxHighlighter(regExp, doc); highlighter->rehighlight(); QAbstractTextDocumentLayout :: PaintContext context; doc->setPageSize(opt.rect.size()); painter->setClipRect(opt.rect); painter->translate( opt.rect.x(), opt.rect.y() ); doc->documentLayout()->draw(painter,context);@
I guess I am missing some minor thing here.
badlogin last edited by
We're (with "Bobs":http://developer.qt.nokia.com/member/711) using some internal code here for a practically same purpose. After a few discussions we published it (and a little sample application) on "gitorious":http://gitorious.org/modelviewutils/modelviewutils.
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.
goetz last edited by
Be aware that the standard delegate on all item views is "QStyledItemDelegate":http://doc.qt.nokia.com/stable/qstyleditemdelegate.html as of Qt 4.4, not "QItemDelegate":http://doc.qt.nokia.com/stable/qitemdelegate.html. See the API docs for the "differences":http://doc.qt.nokia.com/stable/qstyleditemdelegate.html#qstyleditemdelegate-vs-qitemdelegate
Althought this very likely to not solve your actual problem, it is strongly advised to use QStyledItemDelegate as your base class.
cyberbobs last edited by
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.
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
!http://img231.imageshack.us/img231/3166/filter2.png(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 ->
!http://img441.imageshack.us/img441/5984/filterh.png(Filter Example 2)!
If you guys wanna have a look at it - let me know, I will share my code.
[quote author="Volker" date="1295972488"]Be aware that the standard delegate on all item views is "QStyledItemDelegate":http://doc.qt.nokia.com/stable/qstyleditemdelegate.html as of Qt 4.4, not "QItemDelegate":http://doc.qt.nokia.com/stable/qitemdelegate.html. See the API docs for the "differences":http://doc.qt.nokia.com/stable/qstyleditemdelegate.html#qstyleditemdelegate-vs-qitemdelegate
Althought this very likely to not solve your actual problem, it is strongly advised to use QStyledItemDelegate as your base class.[/quote]
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.
perhaps you could make a wiki page for that? perhaps in developement, otherweis in code snippets...
[quote author="Gerolf" date="1295977979"]Ashwin Das
perhaps you could make a wiki page for that? perhaps in developement, otherweis in code snippets...[/quote]
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?
Both is possible, I would just try to replace QItemdelegate with QStyledItemdelegate and try what happens... :-)
zerg2011 last edited by
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 ?
patylu001 last edited by
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?