首页 > 其他分享 >QT实现代码编辑器

QT实现代码编辑器

时间:2023-02-27 12:08:14浏览次数:32  
标签:QT int 代码 CodeEditor lineNumberArea 编辑器 rect top block

#include <QtWidgets>
#include "codeeditor.h"

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

int CodeEditor::lineNumberAreaWidth()
{
int digits = 1;
int max = qMax(1, blockCount());
while (max >= 10) {
max /= 10;
++digits;
}

int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;

return space;
}



void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
{
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}


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


void CodeEditor::resizeEvent(QResizeEvent *e)
{
QPlainTextEdit::resizeEvent(e);

QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}


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



void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), Qt::lightGray);


QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();

while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::black);
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::AlignRight, number);
}

block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
++blockNumber;
}
}

QT实现代码编辑器_QPlainTextEdit

标签:QT,int,代码,CodeEditor,lineNumberArea,编辑器,rect,top,block
From: https://blog.51cto.com/u_15515702/6082211

相关文章

  • Java开发中要避免的坑和一些代码优化技巧
    1:动态SQL遇到的坑,先看下面OGNL表达式的说明。Anyobjectcanbeusedwhereabooleanisrequired.OGNLinterpretsobjectsasbooleanslikethis:Iftheobjecti......
  • Idea 2022 允许一套代码运行多个实例
    在IDEA右上角打开编辑配置  屏幕右侧    增加配置选项  运行多个实例时(比如端口不同),可以通过yml配置文件修改 sever.port=8080  也可以......
  • dokuwiki编辑器修改
    ​ 自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了。一般情况下我们将Word内容粘贴到Web编辑......
  • 几行python代码完美操控手机
    最近一直成谜于python代码带来的便利,今天打算学习下用python代码来控制操作手机,首先需要利用adb,通过安卓调试桥来达到目的,其实也可以用appium来实现,不过appium多数用在自动......
  • 代码整洁之道-函数抽离
    提问如何做函数抽离回答如何实现代码的可读性?即把代码写的像文章一样每个if都抽离出函数并赋予合适的名称比如if(age<18)肯定没有if(IsTeenager())可读性高同理for......
  • vs code 提交代码弹框提示:请确保已在git中配置您的“user.name”和“user.email” —
    修改完项目代码,准备提交到git上,结果提交失败,弹框提示:请确保已在Git中配置您的“user.name”和“user.email”打开终端,配置运行一下命令$gitconfig--globaluser.name......
  • 917~920 异步提交表单,Servlet,Dao,Servlic代码实现
    异步提交表单在此使用异步提交表单是为了获取服务器响应的数据。因为我们前台使用的是html作为视图层,不能够直接从servlet相关的域对象获取值,只能通过ajax获......
  • 【转载】vsCode JS代码格式化插件ESlint
    验证有效,Ctrl+S保存代码全格式化了。写这个主要是个人笔记,算不得重复造。转载自:https://blog.csdn.net/qq_34803821/article/details/849727811、安装插件VSCode中打开......
  • 第三章代码
    importpandasaspdcatering_sale="C:/Users/Lenovo/Desktop/catering_sale.xls"data=pd.read_excel(catering_sale,index_col=u'日期')print(data.describe())importm......
  • 第三章画图代码
    importpandasaspdcatering_sale="C:/Users/Lenovo/Desktop/catering_sale.xls"data=pd.read_excel(catering_sale,index_col=u'日期')print(data.describe())im......