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

 

走马观花

最近的回复

  • @chinasmu Webkit网络的部分可能不是受到Qt控制的,扩展性较差,所以呢,还不能通过代码的方式侦听网络收发数据。但是WebEngine就可以。如果可以的话,试试Qt WebEngine。😺

    阅读更多
  • C

    我用qt建了个对话框程序,里面加了个webkit,请问有办法获取该控件的全部网络封包通信数据吗?
    不采用windows hook recv和send函数的形式,或者有没有第三方的控件可以这样做

    阅读更多
  • 这个错误不是编译器的错误,而是IntelliSense的错误。
    这种情况,可以不用太担心。

    如果你使用MSVC编译应用程序,那么最好安装Qt Visual Studio Addon,使用这个插件同步开发Qt应用程序。
    不过一个小小的建议,就是Qt Creator写Qt程序非常直观,不需要在Visual Studio中那么麻烦,而且Qt Creator是强制安装的,基本上你安装了Qt 5.12,就可以在安装的目录中找到它。QAxContainer是Qt的模块activeQt中的,需要在pro文件中写QT += axcontainer,才会找到这个类。

    阅读更多
  • C

    VS2017 Qt5.12
    新建一个空的QWidget对话框,编译执行
    可以成功生成并执行,但是错误列表里显示

    傲游截图20190217212931.png

    exe文件能够成功生成。

    另外的问题:我在vs2017编译环境中使用qt,需要用到 QAxContainer,我看帮助文档里说要在.pro中加入CONFIG+=qaxcontainer,但是vs2017创建的程序中没有.pro这个文件,那这句话应该加在哪里呢?

    还有我想建一个有浏览器控件的对话框程序,如果想在5.12版本中加入QtWebKit应该如何使用

    阅读更多

关注我们

微博
QQ群











召唤伊斯特瓦尔