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. The text drawn by paint scrolls with the scroll bar

The text drawn by paint scrolls with the scroll bar

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 238 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.
  • K Offline
    K Offline
    KAMblLLl
    wrote on last edited by
    #1

    Hello
    I am using the QItemDelegate to draw text elements in the QListWidget.
    There are some text elements that must have highlighted word inside. I implemented this by separate drawing of one string.
    There is a problem with horizontal scrolling of this text. When I am trying to scroll the text, it moves with the scrollbar.Scrolled.png Not_scrolled.png .
    This is the paint function.
    void paint(QPainter painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    painter->save();
    QString string = index.data().toString();
    QString keyword = qobject_cast<QLineEdit
    >(this->parent())->text();
    if(keyword != "" && string.contains(keyword))
    {
    QRect rect = option.rect;
    rect.setWidth(QFontMetrics(painter->font()).size(Qt::TextSingleLine, string).width());
    int index;
    QString painted = "";
    while(string.contains(keyword))
    {
    index = string.indexOf(keyword);
    QString buffer = string.mid(0,index);

                painter->setPen(Qt::black);
                painter->drawText(rect, buffer);
    
                rect.setX(QFontMetrics(painter->font()).size(Qt::TextSingleLine, buffer + painted).width());
                painter->setPen(Qt::red);
                painter->drawText(rect,keyword);
    
                buffer = buffer + keyword;
                string = string.mid(buffer.size());
                rect.setX(QFontMetrics(painter->font()).size(Qt::TextSingleLine, buffer + painted).width());
                painted =  painted + buffer;
            }
            painter->setPen(Qt::black);
            painter->drawText(rect, string);
        }
        else
        {
            painter->setPen(Qt::black);
            painter->drawText(option.rect, string);
        }
        painter->restore();
    }
    
    jsulmJ 1 Reply Last reply
    0
    • K KAMblLLl

      Hello
      I am using the QItemDelegate to draw text elements in the QListWidget.
      There are some text elements that must have highlighted word inside. I implemented this by separate drawing of one string.
      There is a problem with horizontal scrolling of this text. When I am trying to scroll the text, it moves with the scrollbar.Scrolled.png Not_scrolled.png .
      This is the paint function.
      void paint(QPainter painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
      {
      painter->save();
      QString string = index.data().toString();
      QString keyword = qobject_cast<QLineEdit
      >(this->parent())->text();
      if(keyword != "" && string.contains(keyword))
      {
      QRect rect = option.rect;
      rect.setWidth(QFontMetrics(painter->font()).size(Qt::TextSingleLine, string).width());
      int index;
      QString painted = "";
      while(string.contains(keyword))
      {
      index = string.indexOf(keyword);
      QString buffer = string.mid(0,index);

                  painter->setPen(Qt::black);
                  painter->drawText(rect, buffer);
      
                  rect.setX(QFontMetrics(painter->font()).size(Qt::TextSingleLine, buffer + painted).width());
                  painter->setPen(Qt::red);
                  painter->drawText(rect,keyword);
      
                  buffer = buffer + keyword;
                  string = string.mid(buffer.size());
                  rect.setX(QFontMetrics(painter->font()).size(Qt::TextSingleLine, buffer + painted).width());
                  painted =  painted + buffer;
              }
              painter->setPen(Qt::black);
              painter->drawText(rect, string);
          }
          else
          {
              painter->setPen(Qt::black);
              painter->drawText(option.rect, string);
          }
          painter->restore();
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @KAMblLLl said in The text drawn by paint scrolls with the scroll bar:

      There are some text elements that must have highlighted word inside

      You could also use https://doc.qt.io/qt-6/qsyntaxhighlighter.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      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