qt实现文本高亮

重载QSyntaxHighlighter的highlightBlock

void MyHighlighter::highlightBlock(const QString &text)

{

QTextCharFormat myClassFormat;

myClassFormat.setFontWeight(QFont::Bold);

myClassFormat.setForeground(Qt::darkMagenta);

QString pattern = "\\bMy[A-Za-z]+\\b";

QRegExp expression(pattern);

int index = text.indexOf(expression);

while (index >= 0) {

int length = expression.matchedLength();

setFormat(index, length, myClassFormat);

index = text.indexOf(expression, index + length);

}

}

调用方式:

QTextEdit *editor = new QTextEdit;

MyHighlighter *highlighter = new MyHighlighter(editor->document());

相关推荐
海盗123434 分钟前
C#在Distinct()中使用IEqualityComparer<T>
开发语言·c#
Vertira40 分钟前
python 配置PostgreSQL 数据库
开发语言·python
Highcharts.js1 小时前
Highcharts 纯 JavaScript 图表库深度使用评测
开发语言·前端·javascript·功能测试·ecmascript·highcharts·技术评测
瑶池酒剑仙1 小时前
C++类和对象完全指南:从封装继承多态到内存布局的面向对象宝典(雨夜论道)
c语言·开发语言·c++·visual studio
三品吉他手会点灯1 小时前
C语言学习笔记 - 27.C编程预备计算机专业知识 - 什么是字节
c语言·开发语言·笔记·学习
许彰午1 小时前
政务远程帮办部署踩坑实录——从互联网到政务外网
开发语言·网络·政务
存在的五月雨3 小时前
项目中 Vitest 配置详解:vitest.config.ts
开发语言·javascript·vue.js
野犬寒鸦3 小时前
Claude Code:终端AI编程助手全指南(附带指令全讲解)
开发语言·后端·面试·ai编程
淡笑沐白3 小时前
JavaScript零基础到精通
开发语言·javascript·ecmascript