Qt World Summit: Register Today!

Highlight or color a word of string in tableview

  • 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>");

    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

  • Moderators

    @AmrCoder Did you try setForeground for QStandardItem?

  • 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

  • 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
      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);
      QTextDocument doc;
      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);
    QSize HTMLDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
      QStyleOptionViewItemV4 options = option;
      initStyleOption(&options, index);
      QTextDocument doc;
      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);

    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();

    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

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

  • @VRonin Thank you working great

Log in to reply