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

相关推荐
A黄俊辉A几秒前
axios+ts封装
开发语言·前端·javascript
杨福瑞1 小时前
C语⾔内存函数
c语言·开发语言
eqwaak01 小时前
科技信息差(9.12)
开发语言·python·科技·量子计算
axban1 小时前
QT M/V架构开发实战:QStringListModel介绍
开发语言·数据库·qt
刘媚-海外1 小时前
Go语言开发AI应用
开发语言·人工智能·golang·go
勇敢牛牛_1 小时前
使用Rust实现服务配置/注册中心
开发语言·后端·rust·注册中心·配置中心
catchadmin1 小时前
PHP serialize 序列化完全指南
android·开发语言·php
hzzzzzo02 小时前
微服务网关全解析:从入门到实践
java·开发语言·微服务
slandarer2 小时前
MATLAB | 这是屎吗?抱歉打错了,这是什么?
开发语言·matlab
We....2 小时前
Java多线程分块下载文件
java·开发语言