qt 中的textedit怎么加上行号



  • qt 中的textedit怎么加上行号,即每行前有标号,如1,2,3。。。并且别人删除不了。0_1451983199917_Z8K{U4GHWM(D~YMX6Q27%H7.png 类似这种效果



  • @hitlyj 这个其实是可以做到的。那就是子类化QTextEdit,然后在paintEvent上使用QPainter绘制行号。下面是Qt Creator的相关代码,代码在(D:\qt-creator\src\plugins\texteditor):

    void TextEditorWidget::extraAreaPaintEvent(QPaintEvent *e)
    {
        QTextDocument *doc = document();
        TextDocumentLayout *documentLayout = qobject_cast<TextDocumentLayout*>(doc->documentLayout());
        QTC_ASSERT(documentLayout, return);
    
        int selStart = textCursor().selectionStart();
        int selEnd = textCursor().selectionEnd();
    
        QPalette pal = d->m_extraArea->palette();
        pal.setCurrentColorGroup(QPalette::Active);
        QPainter painter(d->m_extraArea);
        const QFontMetrics fm(d->m_extraArea->font());
        int fmLineSpacing = fm.lineSpacing();
    
        int markWidth = 0;
        if (d->m_marksVisible)
            markWidth += fm.lineSpacing();
    
        const int collapseColumnWidth = d->m_codeFoldingVisible ? foldBoxWidth(fm): 0;
        const int extraAreaWidth = d->m_extraArea->width() - collapseColumnWidth;
    
        painter.fillRect(e->rect(), pal.color(QPalette::Background));
    
        QTextBlock block = firstVisibleBlock();
        int blockNumber = block.blockNumber();
        qreal top = blockBoundingGeometry(block).translated(contentOffset()).top();
        qreal bottom = top;
    
        while (block.isValid() && top <= e->rect().bottom()) {
    
            top = bottom;
            const qreal height = blockBoundingRect(block).height();
            bottom = top + height;
            QTextBlock nextBlock = block.next();
    
            QTextBlock nextVisibleBlock = nextBlock;
            int nextVisibleBlockNumber = blockNumber + 1;
    
            if (!nextVisibleBlock.isVisible()) {
                // invisible blocks do have zero line count
                nextVisibleBlock = doc->findBlockByLineNumber(nextVisibleBlock.firstLineNumber());
                nextVisibleBlockNumber = nextVisibleBlock.blockNumber();
            }
    
            if (bottom < e->rect().top()) {
                block = nextVisibleBlock;
                blockNumber = nextVisibleBlockNumber;
                continue;
            }
    
            painter.setPen(pal.color(QPalette::Dark));
    
            if (d->m_lineNumbersVisible) {
                const QString &number = lineNumber(blockNumber);
                bool selected = (
                        (selStart < block.position() + block.length()
    
                        && selEnd > block.position())
                        || (selStart == selEnd && selStart == block.position())
                        );
                if (selected) {
                    painter.save();
                    QFont f = painter.font();
                    const QTextCharFormat &currentLineNumberFormat
                            = textDocument()->fontSettings().toTextCharFormat(C_CURRENT_LINE_NUMBER);
                    f.setBold(currentLineNumberFormat.font().bold());
                    f.setItalic(currentLineNumberFormat.font().italic());
                    painter.setFont(f);
                    painter.setPen(currentLineNumberFormat.foreground().color());
                    if (currentLineNumberFormat.background() != Qt::NoBrush)
                        painter.fillRect(QRect(0, top, extraAreaWidth, height), currentLineNumberFormat.background().color());
                }
                painter.drawText(QRectF(markWidth, top, extraAreaWidth - markWidth - 4, height), Qt::AlignRight, number);
                if (selected)
                    painter.restore();
            }
    
            if (d->m_codeFoldingVisible || d->m_marksVisible) {
                painter.save();
                painter.setRenderHint(QPainter::Antialiasing, false);
    
                if (TextBlockUserData *userData = static_cast<TextBlockUserData*>(block.userData())) {
                    if (d->m_marksVisible) {
                        int xoffset = 0;
                        TextMarks marks = userData->marks();
                        TextMarks::const_iterator it = marks.constBegin();
                        if (marks.size() > 3) {
                            // We want the 3 with the highest priority so iterate from the back
                            int count = 0;
                            it = marks.constEnd() - 1;
                            while (it != marks.constBegin()) {
                                if ((*it)->isVisible())
                                    ++count;
                                if (count == 3)
                                    break;
                                --it;
                            }
                        }
                        TextMarks::const_iterator end = marks.constEnd();
                        for ( ; it != end; ++it) {
                            TextMark *mark = *it;
                            if (!mark->isVisible())
                                continue;
                            const int height = fmLineSpacing - 1;
                            const int width = int(.5 + height * mark->widthFactor());
                            const QRect r(xoffset, top, width, height);
                            mark->paint(&painter, r);
                            xoffset += 2;
                        }
                    }
                }
    
                if (d->m_codeFoldingVisible) {
    
                    int extraAreaHighlightFoldBlockNumber = -1;
                    int extraAreaHighlightFoldEndBlockNumber = -1;
                    bool endIsVisible = false;
                    if (!d->m_highlightBlocksInfo.isEmpty()) {
                        extraAreaHighlightFoldBlockNumber =  d->m_highlightBlocksInfo.open.last();
                        extraAreaHighlightFoldEndBlockNumber =  d->m_highlightBlocksInfo.close.first();
                        endIsVisible = doc->findBlockByNumber(extraAreaHighlightFoldEndBlockNumber).isVisible();
    
    //                    QTextBlock before = doc->findBlockByNumber(extraAreaHighlightCollapseBlockNumber-1);
    //                    if (TextBlockUserData::hasCollapseAfter(before)) {
    //                        extraAreaHighlightCollapseBlockNumber--;
    //                    }
                    }
    
                    TextBlockUserData *nextBlockUserData = TextDocumentLayout::testUserData(nextBlock);
    
                    bool drawBox = nextBlockUserData
                                   && TextDocumentLayout::foldingIndent(block) < nextBlockUserData->foldingIndent();
    
    
    
                    bool active = blockNumber == extraAreaHighlightFoldBlockNumber;
    
                    bool drawStart = active;
                    bool drawEnd = blockNumber == extraAreaHighlightFoldEndBlockNumber || (drawStart && !endIsVisible);
                    bool hovered = blockNumber >= extraAreaHighlightFoldBlockNumber
                                   && blockNumber <= extraAreaHighlightFoldEndBlockNumber;
    
                    int boxWidth = foldBoxWidth(fm);
                    if (hovered) {
                        int itop = qRound(top);
                        int ibottom = qRound(bottom);
                        QRect box = QRect(extraAreaWidth + 1, itop, boxWidth - 2, ibottom - itop);
                        drawRectBox(&painter, box, drawStart, drawEnd, pal);
                    }
    
                    if (drawBox) {
                        bool expanded = nextBlock.isVisible();
                        int size = boxWidth/4;
                        QRect box(extraAreaWidth + size, top + size,
                                  2 * (size) + 1, 2 * (size) + 1);
                        d->drawFoldingMarker(&painter, pal, box, expanded, active, hovered);
                    }
                }
    
                painter.restore();
            }
    
    
            if (d->m_revisionsVisible && block.revision() != documentLayout->lastSaveRevision) {
                painter.save();
                painter.setRenderHint(QPainter::Antialiasing, false);
                if (block.revision() < 0)
                    painter.setPen(QPen(Qt::darkGreen, 2));
                else
                    painter.setPen(QPen(Qt::red, 2));
                painter.drawLine(extraAreaWidth - 1, top, extraAreaWidth - 1, bottom - 1);
                painter.restore();
            }
    
            block = nextVisibleBlock;
            blockNumber = nextVisibleBlockNumber;
        }
    }
    

 

走马观花

最近的回复

关注我们

微博
QQ群











召唤伊斯特瓦尔