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

 

最近的回复

  • 随后在git for windows这个软件中输入

    git clone git@git.coding.net:jiangcaiyang1/qtdreamrelease_windows.git

    就可以获得一个空的项目了。其它的项目也是同样的操作方法。然后呢,将此作为自动构建的代码库吧。
    此次介绍就是这样~😘 😘 🐧

    阅读更多
  • 0_1540027445466_很好.jpg
    萌梦安装包的分发始终是一个难题。要有一个稳定并且有效的方法进行分包,才能让大家都能够享受到萌梦动作编辑器的最新功能。我们目前有两种分发的方法:

    离线安装包。通常的位置是
    1.1 百度云盘
    1.2 QQ群文件
    1.3 github
    其中百度云盘和QQ群文件不会定期地更新,只有github的文件会定期更新。因为构建脚本要求每次有更改,都让github的文件更新。

    在线安装包。通常的位置是
    2.1 百度云盘
    2.2 QQ群文件
    2.3 github
    这里在线安装包的代码库主要还是在github中,在线安装包非常依赖CDN加速。目前github这块儿在国内做得不太好,还是希望在国内找一个类似的加速。目前可以选的只有两家:

    码云

    coding

    因为以前做过coding的加速,目前coding已经和腾讯云战略合作,所有的代码都要迁移到腾讯云了。而且对于代码库的空间要求变高了。128M。目前我的一个账号显然满足不了要求。所以有必要再注册一个账号。

    下面记录一下新建coding代码库的方法,并且尝试将萌梦安装包的线上代码库部分移动到coding代码库中。

    注册

    到这个位置注册。

    https://coding.net/register

    相信很快就会带你跳转到腾讯云中。不过这里我选择的是邮箱的方式,因为我希望注册尽量多的账号。

    设置ssh密钥

    进入控制台后,是这样的:
    0_1540028446495_2eeb7e93-c15b-4c82-9b36-58e1c304ea3f-image.png
    这个时候,我们要先创建项目,然后ssh的公钥。
    0_1540028822318_b3e81b93-099c-42d5-820d-deaf8d445913-image.png

    创建以后,开始部署公钥。在这里:
    0_1540028925337_d8059b64-8535-45e5-ba4c-d7454505aa5a-image.png
    这期间最好用OpenSSL生成SSH密钥。
    我使用的是git for windows来生成密钥的。方法是

    ssh-keygen

    如果你生成好了密钥,可以直接打开密钥内容看看:

    cat ~/.ssh/id_rsa.pub

    然后粘贴到网站的文本框中。

    添加好了之后,勾选“准许推送权限”,需要推送代码到代码库中。

    阅读更多

关注我们

微博
QQ群











召唤伊斯特瓦尔