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. QTableWidget Highlight Partial Text
QtWS25 Last Chance

QTableWidget Highlight Partial Text

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtablewidgethighlightqt 5.5.1
3 Posts 2 Posters 1.8k 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.
  • P Offline
    P Offline
    Punit
    wrote on 4 Nov 2016, 12:29 last edited by Punit 11 Apr 2016, 12:30
    #1

    Hello all,

    I have a search box (QLineEdit) and a QTableWidget. Now suppose if I enter the word "xyz" in search box than if any QTablewidget cell contains that specific text i.e. "xyz", I want to highlight that part of the text with yellow color. (ctrl+f behavior in QT).

    Please help. I have tried many ways to do this, but not succeeded yet.

    Thanks.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 4 Nov 2016, 12:55 last edited by
      #2

      it's not crazy difficult, you just need to subclass QStyledItemDelegate, add a member to hold the string and reimplement the paint method to do the highlight

      "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
      1
      • P Offline
        P Offline
        Punit
        wrote on 4 Nov 2016, 13:01 last edited by
        #3

        @VRonin

        I am trying to do this. I am a newbiew, so need your assistance. Hope you can understand. Here are some rough code.

        MediaFileItemDelegate::MediaFileItemDelegate(QObject *parent) :QStyledItemDelegate(parent)
        {
        
        }
        
        MediaFileItemDelegate::~MediaFileItemDelegate()
        {
        
        }
        
        void MediaFileItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,const QModelIndex& index) const
        {
            QString str = qvariant_cast<QString>(index.data());
        
            painter->save();
        
            painter->setPen(Qt::NoPen);
        
           // painter->drawRect(option.rect);
        
            if(index.data().toString().contains("xyz"))
            {
                painter->setPen(Qt::blue);
                painter->drawText(option, str);
            }
            else
            {
                QStyledItemDelegate::paint(painter, option, index);
            }
        
            painter->restore();
        }
        

        Currently this code is highlighting the whole text (example: My name is xyz) and I am doing manually matching. So how can i select only xyz. Thanks

        1 Reply Last reply
        0

        2/3

        4 Nov 2016, 12:55

        • Login

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