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

相关推荐
勤奋菲菲29 分钟前
Vue3+Three.js:requestAnimationFrame的详细介绍
开发语言·javascript·three.js·前端可视化
要天天开心啊44 分钟前
Java序列化和反序列化
java·开发语言
二宝1521 小时前
黑马商城day1-MyBatis-Plus
java·开发语言·mybatis
AI+程序员在路上1 小时前
QT6中Combo Box与Combo BoxFont 功能及用法
c++·qt
Porunarufu1 小时前
JAVA·类和对象③封装及包
java·开发语言
煜3642 小时前
C++异常与智能指针
开发语言·c++
wydaicls2 小时前
AIDL 接口的定义与生成,使用
java·开发语言
云草桑2 小时前
C#入坑JAVA 使用XXLJob
java·开发语言·c#
shx66662 小时前
python杂记
开发语言·python
卿摆摆3 小时前
【C++】string的模拟实现
开发语言·c++