Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Highlighting partial text inside QTableView

Highlighting partial text inside QTableView

Scheduled Pinned Locked Moved General and Desktop
15 Posts 8 Posters 14.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ashwindas
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

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

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ashwindas
        wrote on last edited by
        #3

        Thanks Gerolf, I will try doing that.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ashwindas
            wrote on last edited by
            #5

            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;
            doc->setPlainText(text);
            doc->adjustSize();

            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.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              badlogin
              wrote on last edited by
              #6

              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.

              !http://img192.imageshack.us/img192/1912/modelviewutils.png(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.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                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.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  cyberbobs
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    ashwindas
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      ashwindas
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on last edited by
                        #11

                        Ashwin Das

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

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

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          ashwindas
                          wrote on last edited by
                          #12

                          [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?

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            giesbert
                            wrote on last edited by
                            #13

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

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

                            1 Reply Last reply
                            0
                            • Z Offline
                              Z Offline
                              zerg2011
                              wrote on last edited by
                              #14

                              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 ?

                              1 Reply Last reply
                              0
                              • P Offline
                                P Offline
                                patylu001
                                wrote on last edited by
                                #15

                                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?

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved