Understanding Text‑Block Metrics in Qt
Text Block
A QTextBlock
represents one “paragraph” or line of text in a QTextDocument
.
blockBoundingRect()
Returns only the size (width & height) of that block , no positional information.
blockBoundingGeometry()
Returns the block’s full rectangle in the document’s coordinate system, giving both its size and its original position in the unscrolled document.
contentOffset()
Reports the current scroll translation of the viewport (how far the content has shifted).
Geometry + Offset → On‑Screen Position
By translating the block’s document‑coordinates rectangle by the content offset, you compute exactly where that block appears in the visible viewport—essential for drawing aligned overlays (e.g., line numbers, custom highlights).
Note: The black rectangle in the diagram represents the visible portion of the document. In reality, each
QTextBlock
may have a different width depending on its content; we’ve simplified it to a single rectangle here for clarity.