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;
        }
    }
    

Log in to reply
 

走马观花

最近的回复

  • E

    是不是好久都没有人了,站主找到工作了,还是说划水了

    read more
  • 请问一下如果原Qt桌面软件支持拖拽外部文件进去(比如把在用资源管理器里的一张图片拖拽到软件界面上就会显示这张图片),转成Webassembly之后这种拖拽操作还是否有效?劳烦大佬帮我试试看,可以的话我就学QML了

    read more
  • H
    Toou 2D 拿来即用,为简单而生。

    简称T2D,是一款采用自身模块规范编写的轻量级UI框架,遵循Qt书写与组织形式门槛极低无需深入学习简单易用可拿来即用,丰富的控件模块适合界面的快速开发,让程序人员拥有更多的精力来实现业务逻辑与算法。

    统一交互规范,丰富的Ui控件几十种常用控件放弃了Qt Controls 及 Controls 2 来提高性能。

    完善的主题系统,业务逻辑与界面主题设计分离,可通过简单修改变量自定义主题皮肤。灵活的多主题皮肤绑定机制、在不需要重启App即实现一键换肤

    ini皮肤配置规则与每一个控件融合。可在应用内配置也可在应用外动态扩展配置。

    框架自动化安装支持动态库、静态库多模式编译。使用方便更安全更自由。

    提供丰富Demo、全面的帮助文档,Api查阅快速方便。项目必备开源框架!

    已经集成最新版 Font Awesome 4.7

    开源地址:https://github.com/ShowFL/Toou-2D

    read more
  • 刚刚毕业,工作用QT开发,以后有问题多多请教各位前辈😬 抱拳了。

    read more

关注我们

微博
QQ群