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. Highlight or color a word of string in tableview

Highlight or color a word of string in tableview

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.9k 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.
  • AmrCoderA Offline
    AmrCoderA Offline
    AmrCoder
    wrote on last edited by
    #1

    I have a table view in wich I want to highlight or color a word in it I use this code to make the table view

        QStandardItemModel *model = new QStandardItemModel();
        model->setHorizontalHeaderItem(0,new QStandardItem(QString("Name")));
        model->setHorizontalHeaderItem(1,new QStandardItem(QString("Email")));
        QFile file("a.txt"); // contains 7000 name / email , email lenght between 100 500 char
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        QTextStream s(&file);
        while (!s.atEnd()) {
            QString name = s.readLine();
            QString e = s.readLine();
            if (e.contains("good")) {
                // here how to highelight this word !!!!!!!!!!!!!!!!
                // i try this
                int start = e.indexOf("good");
                int end = start + QString("good").size();
                e = e.insert(start,"<font color = \"red\">");
                e = e.insert(end,"</font>");
            }
        }
        ui->tableView->setModel(model);
    

    I try also to add a PlainttextEdit in the column and use QTextCursor + QTextCharFormat to highlight the word
    but it take a very very long time reach to 15 minute to show the table and it crashes I want to solve this by anyway to highlight or color the word prefered to highlight
    is there any way to to this in qt
    Thanks in advance

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @AmrCoder Did you try setForeground for QStandardItem?

      157

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        The default delegate does not support rich text, you have to implement a custom delegate.

        Chapter 5 of "Advanced Qt Programming Creating Great Software with C++ and Qt 4" has a rich text delegate implementation (source code downloadable directly from here) but, imho it's not a great implementation

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3
        • AmrCoderA Offline
          AmrCoderA Offline
          AmrCoder
          wrote on last edited by
          #4

          ok thanks for your reply it helps me but I have a small problem when I search I got this code to set the delegate to my table view and use HTML code to color what I want from words inside the table I use this code

          class HTMLDelegate : public QStyledItemDelegate
          {
          protected:
            void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
            QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
          };
          
          void HTMLDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
          {
            QStyleOptionViewItemV4 options = option;
            initStyleOption(&options, index);
          
            painter->save();
          
            QTextDocument doc;
            doc.setHtml(options.text);
          
            options.text = "";
            options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);
          
            painter->translate(options.rect.left(), options.rect.top());
            QRect clip(0, 0, options.rect.width(), options.rect.height());
            doc.drawContents(painter, clip);
          
            painter->restore();
          }
          
          QSize HTMLDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
          {
            QStyleOptionViewItemV4 options = option;
            initStyleOption(&options, index);
          
            QTextDocument doc;
            doc.setHtml(options.text);
            doc.setTextWidth(options.rect.width());
            return QSize(doc.idealWidth(), doc.size().height());
          }
          

          but after i use this code i use this 2 properties for my tableview

          ui->tableView->horizontalHeader()->setSectionResizeMode( QHeaderView::Stretch);
          ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
          

          so the horizontal stracth so when i resize the tableview it stratched and resize the verticle to content each item with it's content maybe too long maybe short so it appeare like this
          alt text
          when i use the HTMLDelegate class and set it to my tableview like this

          del = new HTMLDelegate();
          ui->tableView->setItemDelegate(del);
          

          it appeare like the this
          alt text
          so what edit i should do to make it have the same properties to use html to color the words and in the same time look like the first one

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            add doc.setTextWidth(options.rect.width()); to paint()

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            3
            • AmrCoderA Offline
              AmrCoderA Offline
              AmrCoder
              wrote on last edited by
              #6

              @VRonin Thank you working great

              1 Reply Last reply
              1

              • Login

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