当前位置:网站首页>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 :

边栏推荐
- Garbage collection of C closure
- WordPress add photo album function [advanced custom fields Pro custom fields plug-in series tutorial]
- Kubernetes 架构核心组件工作原理解析
- The problem of multiple callback of video ads stimulated by applets (offcolse problem)
- 【Redis实现秒杀业务③】超卖问题之乐观锁具体实现
- EVM简略
- JMeter socket connection sends data
- 2022年全国最新消防设施操作员(高级消防设施操作员)模拟题及答案
- Custom animation (simulated win10 loading animation) - Optimization
- Uniapp encapsulated incentive advertisement, screen insert advertisement and banner advertisement
猜你喜欢

Encryption and encoding resolution

How to reduce the font size of custom controls (optimize the round dot progress bar)

Activity startup process

How to quickly open traffic master for wechat applet

Tiktok wallpaper applet source code

Text editor for QT project practice - Episode 10

在企业级开发过程中我发现有位同事用select * from where 条件 for update

Punch smart spirit 1. The brand is attractive. What is the strength of the product?

I brush the question I - copy the linked list with random pointer

Binder mechanism and Aidl communication example
随机推荐
Eliminate duplicate dependencies
Use of navigation and navigationui
I'd like to ask how to open an account at industrial securities? Is it safe to open a stock account through the link
Tiktok wallpaper applet, starlight wallpaper applet version 2.0, upgraded version
Wallpaper applet wechat applet
The acceleration of 100 km is only 5.92 seconds, and the willanda high-performance version leads with the strength of high-energy products
Mobile security tool -dex2jar
最新QQ微信域名防红PHP程序源码+强制跳转打开
智能合约安全审计入门篇 —— delegatecall (2)
C#和C 的CAN通信实验
Binder mechanism and Aidl communication example
How to quickly open traffic master for wechat applet
Several ways for wechat applet to jump to the page are worth collecting
打卡smart精灵#1,品牌不缺吸引力,产品本身实力如何?
Thingsboard - rest API obtains and refreshes tokens
Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download
2021-04-18
2022年起重机司机(限桥式起重机)考试题库模拟考试平台操作
Applet opening traffic master
Punch smart spirit 1. The brand is attractive. What is the strength of the product?