Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-126591

Improve QPlainTextEdit paintEvent performance when lots of blocks are set invisible

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.7.2, 6.8.0 Beta1
    • None
    • Windows 11
    • Linux/X11, Windows

    Description

      The current implementation of QPlainTextEdit's paintEvent method suffers from performance degradation when numerous blocks are marked invisible. This becomes noticeable due to inefficient handling of invisible block skipping, leading to unnecessary computation.

       

      // void QPlainTextEdit::paintEvent(QPaintEvent *e)
      
      while (block.isValid()) {
      
              QRectF r = blockBoundingRect(block).translated(offset);
              QTextLayout *layout = block.layout();
      
              if (!block.isVisible()) {
                  offset.ry() += r.height();
                  block = block.next();
                  continue;
              }
      // ....
      } 

       

      According to blockBoundingRect implementation in the same file and my understanding, when the block is set invisible, the block height should be 0, so offset.ry() += r.height() is useless. Therefore the two lines can be moved below the invisible skipping, avoiding useless blockBoundingRect call.

       

      
      QRectF QPlainTextDocumentLayout::blockBoundingRect(const QTextBlock &block) const
      {
          if (!block.isValid()) { return QRectF(); }
          QTextLayout *tl = block.layout();
          if (!tl->lineCount())
              const_cast<QPlainTextDocumentLayout*>(this)->layoutBlock(block);
          QRectF br;
          if (block.isVisible()) {
              br = QRectF(QPointF(0, 0), tl->boundingRect().bottomRight());
              if (tl->lineCount() == 1)
                  br.setWidth(qMax(br.width(), tl->lineAt(0).naturalTextWidth()));
              qreal margin = document()->documentMargin();
              br.adjust(0, 0, margin, 0);
              if (!block.next().isValid())
                  br.adjust(0, 0, 0, margin);
          }
          return br;
      }
       

       

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            zhouguan23 richard lee
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes