开发者

How to add lines numbers to QTextEdit?

I am writing a Visual Basic IDE, and I need to add lines numbers to QTextEdit and highlight current line. I have found this tutorial, but it is written in Java and I write my pr开发者_高级运维oject in C++.


I know that Qt tutorial recommends using QPlainTextEdit for text editor implementations, and that the question (except as mentioned in the title), is more general than dealing (absolutely) with a QTextEdit widget, but I succeeded in implementing the behaviour (line numbers + current line number highlight), and I think this might be helpful for some people (like me) who really want to keep going with the Rich Text widget, and want to share my implementation (which is far from perfect - quite fast coded...).

LineNumberArea.h : (Same as "QPlainTextEdit" tutorial)

class LineNumberArea : public QWidget
{
    Q_OBJECT

public:
    LineNumberArea(QTextEdit *editor);

    QSize sizeHint() const;

protected:
    void paintEvent(QPaintEvent *event);

private:
    QTextEdit *codeEditor;
};

LineNumberArea.cpp : (Same as "QPlainTextEdit" tutorial)

LineNumberArea::LineNumberArea(QTextEdit *editor) : QWidget(editor) {
    codeEditor = editor;
}

QSize LineNumberArea::sizeHint() const {
    return QSize(((QTextEditHighlighter *)codeEditor)->lineNumberAreaWidth(), 0);
}

void LineNumberArea::paintEvent(QPaintEvent *event) {
    ((QTextEditHighlighter *)codeEditor)->lineNumberAreaPaintEvent(event);
}

>> qtextedithighlighter.h :

class QTextEditHighlighter : public QTextEdit
{
    Q_OBJECT

public:

    explicit QTextEditHighlighter(QWidget *parent = 0);

    int getFirstVisibleBlockId();
    void lineNumberAreaPaintEvent(QPaintEvent *event);
    int lineNumberAreaWidth();

signals:


public slots:

    void resizeEvent(QResizeEvent *e);

private slots:

    void updateLineNumberAreaWidth(int newBlockCount);
    void updateLineNumberArea(QRectF /*rect_f*/);
    void updateLineNumberArea(int /*slider_pos*/);
    void updateLineNumberArea();

private:

    QWidget *lineNumberArea;

};

>> qtextedithighlighter.cpp :

#include "qtextedithighlighter.h"

QTextEditHighlighter::QTextEditHighlighter(QWidget *parent) :
    QTextEdit(parent)
{
    // Line numbers
    lineNumberArea = new LineNumberArea(this);
    ///
    connect(this->document(), SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
    connect(this->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(updateLineNumberArea/*_2*/(int)));
    connect(this, SIGNAL(textChanged()), this, SLOT(updateLineNumberArea()));
    connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateLineNumberArea()));
    ///
    updateLineNumberAreaWidth(0);
}

int QTextEditHighlighter::lineNumberAreaWidth()
{
    int digits = 1;
    int max = qMax(1, this->document()->blockCount());
    while (max >= 10) {
        max /= 10;
        ++digits;
    }

    int space = 13 +  fontMetrics().width(QLatin1Char('9')) * (digits);

    return space;
}

void QTextEditHighlighter::updateLineNumberAreaWidth(int /* newBlockCount */)
{
    setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}


void QTextEditHighlighter::updateLineNumberArea(QRectF /*rect_f*/)
{
    QTextEditHighlighter::updateLineNumberArea();
}
void QTextEditHighlighter::updateLineNumberArea(int /*slider_pos*/)
{
    QTextEditHighlighter::updateLineNumberArea();
}
void QTextEditHighlighter::updateLineNumberArea()
{
    /*
     * When the signal is emitted, the sliderPosition has been adjusted according to the action,
     * but the value has not yet been propagated (meaning the valueChanged() signal was not yet emitted),
     * and the visual display has not been updated. In slots connected to this signal you can thus safely
     * adjust any action by calling setSliderPosition() yourself, based on both the action and the
     * slider's value.
     */
    // Make sure the sliderPosition triggers one last time the valueChanged() signal with the actual value !!!!
    this->verticalScrollBar()->setSliderPosition(this->verticalScrollBar()->sliderPosition());

    // Since "QTextEdit" does not have an "updateRequest(...)" signal, we chose
    // to grab the imformations from "sliderPosition()" and "contentsRect()".
    // See the necessary connections used (Class constructor implementation part).

    QRect rect =  this->contentsRect();
    lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
    updateLineNumberAreaWidth(0);
    //----------
    int dy = this->verticalScrollBar()->sliderPosition();
    if (dy > -1) {
        lineNumberArea->scroll(0, dy);
    }

    // Addjust slider to alway see the number of the currently being edited line...
    int first_block_id = getFirstVisibleBlockId();
    if (first_block_id == 0 || this->textCursor().block().blockNumber() == first_block_id-1)
        this->verticalScrollBar()->setSliderPosition(dy-this->document()->documentMargin());

//    // Snap to first line (TODO...)
//    if (first_block_id > 0)
//    {
//        int slider_pos = this->verticalScrollBar()->sliderPosition();
//        int prev_block_height = (int) this->document()->documentLayout()->blockBoundingRect(this->document()->findBlockByNumber(first_block_id-1)).height();
//        if (dy <= this->document()->documentMargin() + prev_block_height)
//            this->verticalScrollBar()->setSliderPosition(slider_pos - (this->document()->documentMargin() + prev_block_height));
//    }

}


void QTextEditHighlighter::resizeEvent(QResizeEvent *e)
{
    QTextEdit::resizeEvent(e);

    QRect cr = this->contentsRect();
    lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}


int QTextEditHighlighter::getFirstVisibleBlockId()
{
    // Detect the first block for which bounding rect - once translated 
    // in absolute coordinated - is contained by the editor's text area

    // Costly way of doing but since "blockBoundingGeometry(...)" doesn't 
    // exists for "QTextEdit"...

    QTextCursor curs = QTextCursor(this->document());
    curs.movePosition(QTextCursor::Start);
    for(int i=0; i < this->document()->blockCount(); ++i)
    {
        QTextBlock block = curs.block();

        QRect r1 = this->viewport()->geometry();
        QRect r2 = this->document()->documentLayout()->blockBoundingRect(block).translated(
                    this->viewport()->geometry().x(), this->viewport()->geometry().y() - (
                        this->verticalScrollBar()->sliderPosition()
                        ) ).toRect();

        if (r1.contains(r2, true)) { return i; }

        curs.movePosition(QTextCursor::NextBlock);
    }

    return 0;
}

void QTextEditHighlighter::lineNumberAreaPaintEvent(QPaintEvent *event)
{
    this->verticalScrollBar()->setSliderPosition(this->verticalScrollBar()->sliderPosition());

    QPainter painter(lineNumberArea);
    painter.fillRect(event->rect(), Qt::lightGray);
    int blockNumber = this->getFirstVisibleBlockId();

    QTextBlock block = this->document()->findBlockByNumber(blockNumber);
    QTextBlock prev_block = (blockNumber > 0) ? this->document()->findBlockByNumber(blockNumber-1) : block;
    int translate_y = (blockNumber > 0) ? -this->verticalScrollBar()->sliderPosition() : 0;

    int top = this->viewport()->geometry().top();

    // Adjust text position according to the previous "non entirely visible" block 
    // if applicable. Also takes in consideration the document's margin offset.
    int additional_margin;
    if (blockNumber == 0)
        // Simply adjust to document's margin
        additional_margin = (int) this->document()->documentMargin() -1 - this->verticalScrollBar()->sliderPosition();
    else
        // Getting the height of the visible part of the previous "non entirely visible" block
        additional_margin = (int) this->document()->documentLayout()->blockBoundingRect(prev_block)
                .translated(0, translate_y).intersect(this->viewport()->geometry()).height();

    // Shift the starting point
    top += additional_margin;

    int bottom = top + (int) this->document()->documentLayout()->blockBoundingRect(block).height();

    QColor col_1(90, 255, 30);      // Current line (custom green)
    QColor col_0(120, 120, 120);    // Other lines  (custom darkgrey)

    // Draw the numbers (displaying the current line number in green)
    while (block.isValid() && top <= event->rect().bottom()) {
        if (block.isVisible() && bottom >= event->rect().top()) {
            QString number = QString::number(blockNumber + 1);
            painter.setPen(QColor(120, 120, 120));
            painter.setPen((this->textCursor().blockNumber() == blockNumber) ? col_1 : col_0);
            painter.drawText(-5, top,
                             lineNumberArea->width(), fontMetrics().height(),
                             Qt::AlignRight, number);
        }

        block = block.next();
        top = bottom;
        bottom = top + (int) this->document()->documentLayout()->blockBoundingRect(block).height();
        ++blockNumber;
    }

}

Hope this can help...


Here's the equivalent tutorial in C++:

Qt4: http://doc.qt.io/qt-4.8/qt-widgets-codeeditor-example.html

Qt5: http://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html


I was looking for a line numbers painting solution for QTextEdit (not QPlainTextEdit), and I found the previous answer with sample code for QTextEdit is useful, but when we set custom line height in QTextEdit's associated SyntaxHighligher, it doesn't work reliably.

To fix that problem, I figured out a simpler way to determine the y coordinate of each block rect by using this code:

// Here is the key to obtain the y coordinate of the block start
QTextCursor blockCursor(block);
QRect blockCursorRect = this->cursorRect(blockCursor);

And then we can draw line number of each block via:

painter.drawText(-5, blockCursorRect.y() /* + a little offset to align */,
                 m_lineNumberArea->width(), fixedLineHeight,
                 Qt::AlignRight, number);

This seems much simpler and more reliable than calculating the block y coordinate by adding previous block height up.

Hope it helps for someone who is looking for similar solutions.


Here is an easy example to determine the text positions for drawing line numbers for the QTextEdit derived classes.

code_browser::code_browser(QWidget *parent): QTextBrowser(parent)
  , m_line_number_area(new LineNumberArea(this))
  , m_show_line_numbers(false)
{
    connect(document(), SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
    connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(vertical_scroll_value(int)));
    connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
    connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));

    updateLineNumberAreaWidth(blockCount());
    highlightCurrentLine();
}

void code_browser::vertical_scroll_value(int value)
{
    Q_EMIT updateRequest(contentsRect(), value);
}

void code_browser::lineNumberAreaPaintEvent(QPaintEvent *event)
{
    QPainter painter(m_line_number_area);
    painter.fillRect(event->rect(), Qt::lightGray);

    int top = 0;
    QTextBlock block = firstVisibleBlock(top);
    int blockNumber = block.blockNumber();

    QRectF block_rect = blockBoundingRect(block);
    int bottom = top + qRound(block_rect.height());
    while (block.isValid() && top <= event->rect().bottom())
    {
        if (block.isVisible() && bottom >= event->rect().top())
        {
            QString number = QString::number(blockNumber + 1);
            painter.setPen(Qt::black);
            painter.drawText(0, top, m_line_number_area->width(), fontMetrics().height(), Qt::AlignRight, number);
        }

        block = block.next();
        top = bottom;
        block_rect = blockBoundingRect(block);
        bottom = top + qRound(block_rect.height());
        ++blockNumber;
    }
}

int code_browser::blockCount() const
{
    return document()->blockCount();
}

QTextBlock code_browser::firstVisibleBlock(int& diff)
{
    QPointF content_offset = contentOffset();
    for (QTextBlock block = document()->begin(); block.isValid(); block = block.next())
    {
        if (block.isVisible())
        {
            QRectF block_rect = blockBoundingRect(block);
            if (block_rect.top() >= content_offset.y())
            {
                diff = block_rect.top() - content_offset.y();
                return block;
            }
        }
    }
    diff = -1;
    return document()->begin();
}

QRectF code_browser::blockBoundingRect(const QTextBlock &block) const
{
    QAbstractTextDocumentLayout *layout = document()->documentLayout();
    return layout->blockBoundingRect(block);
}

    
QPointF code_browser::contentOffset() const
{
    return QPointF(horizontalScrollBar()->value(), verticalScrollBar()->value());
}


Python's adaptation:

QTextEditHighlighter.py

from PySide6.QtCore import QRectF, QRect, Qt
from PySide6.QtGui import QResizeEvent, QTextCursor, QPaintEvent, QPainter, QColor
from PySide6.QtWidgets import QTextEdit, QApplication

from LineNumberArea import LineNumberArea


class QTextEditHighlighter(QTextEdit):
    def __init__(self):
        # Line numbers
        QTextEdit.__init__(self)
        self.lineNumberArea = LineNumberArea(self)

        self.document().blockCountChanged.connect(self.updateLineNumberAreaWidth)
        self.verticalScrollBar().valueChanged.connect(self.updateLineNumberArea)
        self.textChanged.connect(self.updateLineNumberArea)
        self.cursorPositionChanged.connect(self.updateLineNumberArea)

        self.updateLineNumberAreaWidth(0)

    def lineNumberAreaWidth(self):
        digits = 1
        m = max(1, self.document().blockCount())
        while m >= 10:
            m /= 10
            digits += 1
        space = 13 + self.fontMetrics().horizontalAdvance('9') * digits
        return space

    def updateLineNumberAreaWidth(self, newBlockCount: int):
        self.setViewportMargins(self.lineNumberAreaWidth(), 0, 0, 0)

    def updateLineNumberAreaRect(self, rect_f: QRectF):
        self.updateLineNumberArea()

    def updateLineNumberAreaInt(self, slider_pos: int):
        self.updateLineNumberArea()

    def updateLineNumberArea(self):
        """        
        When the signal is emitted, the sliderPosition has been adjusted according to the action,
        but the value has not yet been propagated (meaning the valueChanged() signal was not yet emitted),
        and the visual display has not been updated. In slots connected to self signal you can thus safely
        adjust any action by calling setSliderPosition() yourself, based on both the action and the
        slider's value.
        """
        
        # Make sure the sliderPosition triggers one last time the valueChanged() signal with the actual value !!!!
        self.verticalScrollBar().setSliderPosition(self.verticalScrollBar().sliderPosition())
    
        # Since "QTextEdit" does not have an "updateRequest(...)" signal, we chose
        # to grab the imformations from "sliderPosition()" and "contentsRect()".
        # See the necessary connections used (Class constructor implementation part).
    
        rect = self.contentsRect()

        self.lineNumberArea.update(0, rect.y(), self.lineNumberArea.width(), rect.height())
        self.updateLineNumberAreaWidth(0)
        
        dy = self.verticalScrollBar().sliderPosition()
        if dy > -1:
            self.lineNumberArea.scroll(0, dy)
    
        # Addjust slider to alway see the number of the currently being edited line...
        first_block_id = self.getFirstVisibleBlockId()
        if first_block_id == 0 or self.textCursor().block().blockNumber() == first_block_id-1:
            self.verticalScrollBar().setSliderPosition(dy-self.document().documentMargin())
    
    #    # Snap to first line (TODO...)
    #    if first_block_id > 0:
    #        slider_pos = self.verticalScrollBar().sliderPosition()
    #        prev_block_height = (int) self.document().documentLayout().blockBoundingRect(self.document().findBlockByNumber(first_block_id-1)).height()
    #        if (dy <= self.document().documentMargin() + prev_block_height)
    #            self.verticalScrollBar().setSliderPosition(slider_pos - (self.document().documentMargin() + prev_block_height))

    def resizeEvent(self, event: QResizeEvent):
        QTextEdit.resizeEvent(self, event)

        cr = self.contentsRect()
        self.lineNumberArea.setGeometry(QRect(cr.left(), cr.top(), self.lineNumberAreaWidth(), cr.height()))

    def getFirstVisibleBlockId(self) -> int:
        # Detect the first block for which bounding rect - once translated
        # in absolute coordinated - is contained by the editor's text area
    
        # Costly way of doing but since "blockBoundingGeometry(...)" doesn't
        # exists for "QTextEdit"...
    
        curs = QTextCursor(self.document())
        curs.movePosition(QTextCursor.Start)
        for i in range(self.document().blockCount()):
            block = curs.block()
    
            r1 = self.viewport().geometry()
            r2 = self.document().documentLayout().blockBoundingRect(block).translated(
                    self.viewport().geometry().x(), self.viewport().geometry().y() - (
                        self.verticalScrollBar().sliderPosition()
                        )).toRect()
    
            if r1.contains(r2, True):
                return i
    
            curs.movePosition(QTextCursor.NextBlock)
        return 0
    
    def lineNumberAreaPaintEvent(self, event: QPaintEvent):
        self.verticalScrollBar().setSliderPosition(self.verticalScrollBar().sliderPosition())
    
        painter = QPainter(self.lineNumberArea)
        painter.fillRect(event.rect(), Qt.lightGray)
        blockNumber = self.getFirstVisibleBlockId()
    
        block = self.document().findBlockByNumber(blockNumber)

        if blockNumber > 0:
            prev_block = self.document().findBlockByNumber(blockNumber - 1)
        else:
            prev_block = block

        if blockNumber > 0:
            translate_y = -self.verticalScrollBar().sliderPosition()
        else:
            translate_y = 0
    
        top = self.viewport().geometry().top()
    
        # Adjust text position according to the previous "non entirely visible" block
        # if applicable. Also takes in consideration the document's margin offset.

        if blockNumber == 0:
            # Simply adjust to document's margin
            additional_margin = self.document().documentMargin() -1 - self.verticalScrollBar().sliderPosition()
        else:
            # Getting the height of the visible part of the previous "non entirely visible" block
            additional_margin = self.document().documentLayout().blockBoundingRect(prev_block) \
                    .translated(0, translate_y).intersect(self.viewport().geometry()).height()
    
        # Shift the starting point
        top += additional_margin
    
        bottom = top + int(self.document().documentLayout().blockBoundingRect(block).height())
    
        col_1 = QColor(90, 255, 30)      # Current line (custom green)
        col_0 = QColor(120, 120, 120)    # Other lines  (custom darkgrey)
    
        # Draw the numbers (displaying the current line number in green)
        while block.isValid() and top <= event.rect().bottom():
            if block.isVisible() and bottom >= event.rect().top():
                number = f"{blockNumber + 1}"
                painter.setPen(QColor(120, 120, 120))

                if self.textCursor().blockNumber() == blockNumber:
                    painter.setPen(col_1)
                else:
                    painter.setPen(col_0)

                painter.drawText(-5, top,
                                 self.lineNumberArea.width(), self.fontMetrics().height(),
                                 Qt.AlignRight, number)

            block = block.next()
            top = bottom
            bottom = top + int(self.document().documentLayout().blockBoundingRect(block).height())
            blockNumber += 1


if __name__ == '__main__':
    app = QApplication([])
    w = QTextEditHighlighter()
    w.show()
    app.exec()

LineNumberArea.py

from PySide6.QtCore import QSize
from PySide6.QtWidgets import QWidget


class LineNumberArea(QWidget):
    def __init__(self, editor):
        QWidget.__init__(self, editor)
        self.codeEditor = editor

    def sizeHint(self) -> QSize:
        return QSize(self.codeEditor.lineNumberAreaWidth(), 0)

    def paintEvent(self, event):
        self.codeEditor.lineNumberAreaPaintEvent(event)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜