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

相关推荐
Rsun0455120 小时前
10、Java 桥接模式从入门到实战
java·开发语言·桥接模式
jieyucx21 小时前
Golang 完整安装与 VSCode 开发环境搭建教程
开发语言·vscode·golang
pearlthriving21 小时前
c++当中的泛型思想以及c++11部分新特性
java·开发语言·c++
智慧地球(AI·Earth)21 小时前
规则引擎实战:Python中re库和pyknow库规则引擎实战教程
开发语言·python·程序人生
小雅痞21 小时前
[Java][Leetcode hard] 42. 接雨水
java·开发语言·leetcode
We་ct21 小时前
AI辅助开发术语体系深度剖析
开发语言·前端·人工智能·ai·ai编程
t***54421 小时前
Dev-C++中哪些选项可以设置
开发语言·c++
輕華21 小时前
PyQt5入门实战:安装、QtDesigner设计与PyUIC转换完整指南
开发语言·qt
麻辣璐璐1 天前
EditText属性运用之适配RTL语言和LTR语言的输入习惯
android·xml·java·开发语言·安卓
2301_803554521 天前
C++ 并发核心:std::promise、std::future、std::async 超详细全解
开发语言·c++