代码编辑器主要是使用了CodeEditor和LineNumberArea,其实现步骤如下: CodeEditor是继承QPlainTextEdit的小部件,在CodeEditor(LineNumberArea)中保留一个单独的小部件,在其上绘制行号。 QPlainTextEdit继承自QAbstractScrollArea,并且编辑在其viewport()的边距内进行。通过将视口的左边距设置为绘制行号所需的尺寸,为行号区...
1#include"codeeditor.h"2#include <QDebug>34CodeEditor::CodeEditor(QWidget *parent): QPlainTextEdit(parent)5{6lineNumberArea =newLineNumberArea(this);78//事件绑定9connect(this, SIGNAL(blockCountChanged(int)),this, SLOT(updateLineNumberAreaWidth(int)));10connect(this, SIGNAL(updateRequest(QR...
classLineNumberArea :publicQWidget{public: LineNumberArea(CodeEditor*editor) :QWidget(editor),codeEditor(editor) {}QSizesizeHint()constoverride {returnQSize(codeEditor->lineNumberAreaWidth(),0); }protected:voidpaintEvent(QPaintEvent*event) override { codeEditor->lineNumberAreaPaintEvent(event); }pr...
} voidCodeEditor::updateLineNumberAreaWidth(int/* newBlockCount */) { setViewportMargins(lineNumberAreaWidth(),0,0,0); } voidCodeEditor::updateLineNumberArea(constQRect&rect,intdy) { if(dy) lineNumberArea->scroll(0,dy); else lineNumberArea->update(0,rect.y(),lineNumberArea->width(),...
基于Qt的CodeEditor,首先看最终效果:主要要实现的地方是行号的显示,还有选中行的高亮。项目结构整个程序只有三个文件,最主要的只有一个CodeEditor类,它是继承自QPlainTextEdit,这个类相比于普通的TextEdit更适合于做富文本编辑器。头文件//codeeditor.h#ifndefCODEEDIT
Clone repo: git clone https://github.com/Megaxela/QCodeEditor Go into repo: cd QCodeEditor Create build folder: mkdir build Go into build folder: cd build Generate build file for your compiler: cmake .. If you need to build example, specify -DBUILD_EXAMPLE=On on this step. Build ...
Qt Code Editor Widget It's a widget for editing/viewing code. This project uses a resource namedqcodeeditor_resources.qrc. The main application must not use a resource file with the same name. (It's not a project from a Qt example.) ...
49. QCodeEditor (github.com/rgladwell/Qt): QCodeEditor是一个用于编辑代码的控件,具有语法高亮、自动完成、代码折叠等功能。 50. QTermWidget (github.com/qterminal/qt): QTermWidget是一个用于在Qt应用程序中嵌入终端模拟器的库,支持远程登录和命令执行。发布...
centralSplitter->addWidget(codeEditor); centralSplitter->addWidget(preview); 首先new一个QSplitter,然后将其设置为中间组件,然后在QSplitter上再添加两个组件,分别为代码编辑区域和markdown预览区域的组件。 这两个区域在文章下面将有具体讲解。 底部则是statusBar: ...
CodeEditor 类,继承QPlainTextEdit,更新行号,加载文本,文件操作等。 CodeHighLight 类继承QSyntaxHighlighter,实现关键字、特殊语法等的高亮。 Widget类, UI层操作。 1. 继承 QPlainTextEdit 添加一些功能 行号区域是一个单独的小部件,我们再这个部件上“画”出行号,当文本行数变化时,行号区域的宽度也要发生变化,此...