当前位置:网站首页>Syntax highlighting of rich text
Syntax highlighting of rich text
2022-06-25 01:02:00 【PureヾChan】
When we implement syntax highlighting Qt A class of its own , yes QSyntaxHighlighter Class implementation , When outputting words, search for keywords and set them to highlight .
Add new file :

Customization files , take QSyntaxHighlighter As the base class
MySyntaxHighlighter The header file :
#ifndef MYSYNTAXHIGHLIGHTER_H
#define MYSYNTAXHIGHLIGHTER_H
#include<QSyntaxHighlighter>
class MySyntaxHighlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
MySyntaxHighlighter(QTextDocument* parent=0);
protected:
void highlightBlock(const QString&text);
};
#endif // MYSYNTAXHIGHLIGHTER_H
Add meta objects Q_object, Add parameters QTextDocument* parent, Used to initialize the parent class .
Override the highlight function highlightBlock()
MySyntaxHighlighter Source file :
#include "mysyntaxhighlighter.h"
MySyntaxHighlighter::MySyntaxHighlighter(QTextDocument* parent):QSyntaxHighlighter(parent)
{
}
// Override the highlighting method
void MySyntaxHighlighter::highlightBlock(const QString &text)
{
QTextCharFormat format;// Define a character format
format.setFontWeight(QFont::Bold);
format.setBackground(Qt::lightGray);
format.setForeground(Qt::blue);
QString pattern="\\bgood\\b";// take good As a keyword
QRegExp expression(pattern);
int index=text.indexOf(expression);// Match text to expression , If it matches, it returns the position where the character appears , Return on no -1
while(index>=0){
int length=expression.matchedLength();// The length of the matched characters
setFormat(index,length,format);
index=text.indexOf(expression,index+length);
}
}
Highlight override , First set a character format object format, Further set the font format , After setting, define a string pattern matching good,\\b Represents the font boundary , I'll use it again QRegExp Define a regular expression object ,indexof Returns the first match to the specified subscript string , If there is no match, return -1 End the lecture . The highlight operation is still inseparable setFormat function .
stay mainwindow Add a member to the header file :
MySyntaxHighlighter *m_slighter;// Syntax highlighting objects mainwindow Constructor adds code :
// Initialize highlighted objects
m_slighter=new MySyntaxHighlighter(ui->textEdit->document());Highlight effect :

边栏推荐
- Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
- The drawableleft of the custom textview in kotlin is displayed in the center together with the text
- Binder mechanism and Aidl communication example
- Meta & Berkeley proposed a universal multi-scale visual transformer based on pooled self attention mechanism. The classification accuracy in Imagenet reached 88.8%! Open source
- QT (35) - operate excel qxlsx qaxobject
- Go语言运算符(第8课下)
- 2021-11-07
- Mobile security tool apktool
- 2019 summary and 2020 outlook
- Tiktok wallpaper applet source code
猜你喜欢
最新QQ微信域名防红PHP程序源码+强制跳转打开

【Redis实现秒杀业务③】超卖问题之乐观锁具体实现

Simulation questions and answers of the latest national fire facility operator (senior fire facility operator) in 2022

【微服务|Sentinel】簇点链路|微服务集群环境搭建

打卡smart精灵#1,品牌不缺吸引力,产品本身实力如何?

Punch smart spirit 1. The brand is attractive. What is the strength of the product?
![[microservices sentinel] cluster link | microservices cluster environment construction](/img/75/3cc6abf1e72d651b082415db9db46c.png)
[microservices sentinel] cluster link | microservices cluster environment construction
More pictures | explain the Nacos parameters in detail!

VNC viewer remote connection raspberry pie without display

百公里加速仅5.92秒,威兰达高性能版以高能产品实力领跑
随机推荐
在企业级开发过程中我发现有位同事用select * from where 条件 for update
Use of JMeter easynmon
Paint rounded rectangle
Use coordinatorlayout+appbarlayout+collapsingtoolbarlayout to create a collapsed status bar
2022熔化焊接与热切割复训题库模拟考试平台操作
Xcode preview displays a bug in the content of the list view and its solution
How to reduce the font size of custom controls (optimize the round dot progress bar)
Activity startup process
[Solved] Public key for mysql-community-xxx. rpm is not installed
Wallpaper applet wechat applet
adb shell sendevent
ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’
【微服务|Sentinel】实时监控|RT|吞吐量|并发数|QPS
Punch smart spirit 1. The brand is attractive. What is the strength of the product?
Qiniu cloud uploads video to get the first frame of video
If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?
Technologie des fenêtres coulissantes en octets dans la couche de transmission
Mobile security tool jar
2021-11-07
Meta & Berkeley proposed a universal multi-scale visual transformer based on pooled self attention mechanism. The classification accuracy in Imagenet reached 88.8%! Open source