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

相关推荐
Q_Q51100828517 分钟前
python的校园研招网系统
开发语言·spring boot·python·django·flask·node.js·php
Cyclic10013 小时前
IOS购买订阅通知信息解析说明Java
java·开发语言·ios
AI视觉网奇4 小时前
麒麟系统播放图片 速度比较
开发语言·python·pygame
晨曦5432104 小时前
图(Graph):关系网络的数学抽象
开发语言·算法·php
Ustinian_3105 小时前
【C/C++】For 循环展开与性能优化【附代码讲解】
c语言·开发语言·c++
牵牛老人5 小时前
Qt 插件开发全解析:从接口定义,插件封装,插件调用到插件间的通信
开发语言·qt
钮钴禄·爱因斯晨5 小时前
AIGC浪潮下,风靡全球的Mcp到底是什么?一文讲懂,技术小白都知道!!
开发语言·人工智能·深度学习·神经网络·生成对抗网络·aigc
22jimmy6 小时前
JavaWeb(二)CSS
java·开发语言·前端·css·入门·基础
机器视觉知识推荐、就业指导8 小时前
面试问题详解五:Qt 信号与槽的动态管理
开发语言·qt
四维碎片14 小时前
【Qt】线程池与全局信号实现异步协作
开发语言·qt·ui·visual studio