带行号的QPlainTextEdit

简介: 带行号的QPlainTextEdit
//CodeEditor.h
#ifndef CODEEDITOR_H
#define CODEEDITOR_H
#include <QPlainTextEdit>
#include <QObject>
#include <QSize>
#include <QPaintEvent>
#include <QResizeEvent>
QT_BEGIN_NAMESPACE
class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;
QT_END_NAMESPACE
class LineNumberArea;
class CodeEditor : public QPlainTextEdit
{
    Q_OBJECT
public:
    CodeEditor(QWidget *parent);
    void lineNumberAreaPaintEvent(QPaintEvent *event);
    int lineNumberAreaWidth();
protected:
    void resizeEvent(QResizeEvent *event) override;
private slots:
    void updateLineNumberAreaWidth(int newBlockCount);
    void highlightCurrentLine();
    void updateLineNumberArea(const QRect &, int);
private:
    QWidget *lineNumberArea;
};
class LineNumberArea : public QWidget
{
  Q_OBJECT
public:
    LineNumberArea(CodeEditor *editor) : QWidget(editor) {
        codeEditor = editor;
    }
    QSize sizeHint() const override {
        return QSize(codeEditor->lineNumberAreaWidth(), 0);
    }
protected:
    void paintEvent(QPaintEvent *event) override {
        codeEditor->lineNumberAreaPaintEvent(event);
    }
private:
    CodeEditor *codeEditor;
};
#endif
//CodeEditor.cpp
#include <QtWidgets>
#include "codeeditor.h"
//![constructor]
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
{
    lineNumberArea = new LineNumberArea(this);
    connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
    connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
    connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
    updateLineNumberAreaWidth(0);
    highlightCurrentLine();
}
//![constructor]
//![extraAreaWidth]
int CodeEditor::lineNumberAreaWidth()
{
    int digits = 1;
    int max = qMax(1, blockCount());
    while (max >= 10) {
        max /= 10;
        ++digits;
    }
    int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
    return space;
}
//![extraAreaWidth]
//![slotUpdateExtraAreaWidth]
void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
{
    setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
//![slotUpdateExtraAreaWidth]
//![slotUpdateRequest]
void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
{
    if (dy)
        lineNumberArea->scroll(0, dy);
    else
        lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
    if (rect.contains(viewport()->rect()))
        updateLineNumberAreaWidth(0);
}
//![slotUpdateRequest]
//![resizeEvent]
void CodeEditor::resizeEvent(QResizeEvent *e)
{
    QPlainTextEdit::resizeEvent(e);
    QRect cr = contentsRect();
    lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}
//![resizeEvent]
//![cursorPositionChanged]
void CodeEditor::highlightCurrentLine()
{
    QList<QTextEdit::ExtraSelection> extraSelections;
    if (!isReadOnly()) {
        QTextEdit::ExtraSelection selection;
        QColor lineColor = QColor(Qt::yellow).lighter(160);
        selection.format.setBackground(lineColor);
        selection.format.setProperty(QTextFormat::FullWidthSelection, true);
        selection.cursor = textCursor();
        selection.cursor.clearSelection();
        extraSelections.append(selection);
    }
    setExtraSelections(extraSelections);
}
//![cursorPositionChanged]
//![extraAreaPaintEvent_0]
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
    QPainter painter(lineNumberArea);
    painter.fillRect(event->rect(), Qt::gray);
//![extraAreaPaintEvent_0]
//![extraAreaPaintEvent_1]
    QTextBlock block = firstVisibleBlock();
    int blockNumber = block.blockNumber();
    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    int bottom = top + (int) blockBoundingRect(block).height();
//![extraAreaPaintEvent_1]
//![extraAreaPaintEvent_2]
    while (block.isValid() && top <= event->rect().bottom()) {
        if (block.isVisible() && bottom >= event->rect().top()) {
            QString number = QString::number(blockNumber + 1);
            painter.setPen(Qt::white);
            painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
                             Qt::AlignRight, number);
        }
        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        ++blockNumber;
    }
}
//![extraAreaPaintEvent_2]


相关文章
QT软件开发: QPlainTextEdit当做日志显示窗口
QT软件开发: QPlainTextEdit当做日志显示窗口
1329 0
QT软件开发: QPlainTextEdit当做日志显示窗口
QString与char *之间的完美转换,支持含有中文字符的情况
QString与char *之间的完美转换,支持含有中文字符的情况
3034 0
|
4月前
|
人工智能 自然语言处理 测试技术
Cline(VSCode 智能编程插件)完整实操:对接百炼 Coding Plan 与 Token Plan 全流程教程
随着AI辅助开发不断进化,传统代码补全插件已经难以满足复杂工程的开发诉求。普通补全工具大多只能生成局部代码片段,无法完成需求拆解、多文件修改、终端执行命令、自动化测试等完整工作。Cline是一款运行于VSCode环境的开源AI编程代理插件,区别于普通的代码补全工具,它拥有任务规划、文件读写、终端命令执行、浏览器自动化、MCP工具扩展整套能力,可以把自然语言需求转化成完整可运行的项目代码。同时插件完整兼容OpenAI兼容协议,能够无缝对接百炼平台,支持Coding Plan与Token Plan两套订阅方案,开发者可以直接在编辑器内部调用通义千问系列大模型,实现从需求描述、代码生成、调试运行到测
1261 0
|
4月前
|
人工智能 IDE API
最新版发布通义千问(Qwen3.8-Max)功能介绍
通义千问Qwen3.8-Max是阿里云千问团队推出的最新一代旗舰大模型,总参数量达2.4万亿,采用第三代MoE(混合专家)架构,是千问系列首个突破万亿参数的原生多模态模型。该模型在编程、专业办公、长程智能体、多模态理解等核心领域实现全面跃升,具备从零搭建完整工程、自主执行数十天复杂任务、深度解析图文视频混合素材等突破性能力,综合表现跻身全球大模型第一梯队。本文将从核心架构、能力升级、全场景功能、API接入与实战代码、应用价值五大维度,全面解析Qwen3.8-Max的核心功能与落地方法,助力开发者与企业高效使用这款旗舰模型。
3949 2
|
网络协议 Ubuntu Java
技术笔记:NML工程入门
技术笔记:NML工程入门
|
C语言
LibreCAD+Mingw编译记录
本文记录了使用MinGW编译LibreCAD的过程,包括编译成功截图、编译脚本的修改以及相关参考链接。文章提供了详细的批处理脚本,用于设置编译环境、清理构建、编译程序以及部署安装包。
643 1
LibreCAD+Mingw编译记录
|
UED
【Qt 学习笔记】Qt窗口 | 工具栏 | QToolBar的使用及说明
【Qt 学习笔记】Qt窗口 | 工具栏 | QToolBar的使用及说明
3187 2
|
数据可视化
【Qt 学习笔记】Qt常用控件 | 输入类控件 | Date/Time Edit的使用及说明
【Qt 学习笔记】Qt常用控件 | 输入类控件 | Date/Time Edit的使用及说明
2077 2
【Qt 学习笔记】Qt常用控件 | 显示类控件 | Progress Bar的使用及说明
【Qt 学习笔记】Qt常用控件 | 显示类控件 | Progress Bar的使用及说明
3398 0
|
Linux 编译器 测试技术
探索Linux中的objcopy命令:数据处理与分析的得力助手
`objcopy`是GNU工具集中的实用程序,用于复制和转换二进制目标文件,如ELF到S-record。它支持格式转换、内容提取和修改,如移除调试信息。命令参数包括指定输入/输出格式和复制特定段。示例用途有:`objcopy -O binary input.elf output.bin`(ELF转二进制)和`objcopy -j .text input.elf output.o`(复制.text段)。使用时注意文件格式、备份原始文件并查阅文档。对于处理和分析二进制数据,`objcopy`是不可或缺的工具。