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. Handle QTextBlock with multples lines (Html) to display line numbers
Qt 6.11 is out! See what's new in the release blog

Handle QTextBlock with multples lines (Html) to display line numbers

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 908 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.
  • S Offline
    S Offline
    skmCry
    wrote on last edited by
    #1

    Hello,

    I've created a text editor quite similar to this one (Qt 4.8):
    https://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html

    Primarily it consists of a QPlainTextEdit and a line number bar on the left. The QPlainTextEdit might also
    contain text formatted as HTML (added with appendHthml() function).

    If the QPlainTextEdit contains plain text, the line numbers are correct and each text line is in its own QTextBlock.
    Therefore the function lineNumberAreaPaintEvent() (see example above) works correclty.
    But once HTML formatted text is added, QPlainTextEdit consists of a single QTextBlock that contains all text.
    Therefore the function lineNumberAreaPaintEvent() does not work correctly anymore.

    The following code of the function lineNumberAreaPaintEvent()

    while (block.isValid() && top <= event->rect().bottom()) {
            if (block.isVisible() && bottom >= event->rect().top()) {
    //....
            }
    
            block = block.next();
            top = bottom;
            bottom = top + qRound(blockBoundingRect(block).height());
            ++blockNumber;
    }
    

    is very important to prevent unnecessary draw events that will otherwise hurt scroll performance when working with very large documents. Is there maybe a way to have every line of the HTML formatted text in its own QTextBlock so that the function will work again? Or can you find out which lines of a QTextBlock are currently displyed to paint only the necessary line numbers?

    Thank you very much
    skmCry

    1 Reply Last reply
    0
    • S Offline
      S Offline
      skmCry
      wrote on last edited by
      #2

      For everyone who is interested; The above example/text editor works correctly if the Html text is formatted correclty. Each line must be in its own paragraph (<p> tag), then Qt is able to create invidiual QTextBlock objects for each line. <br> does not work.

      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