-
QT手冊中關於TextEdit和QSyntaxHighlighter介紹如下:
TextEdit QML Type
QSyntaxHighlighter Class -
語法高亮的實現關鍵描述如下圖:
就是說TextEdit的textDocument屬性返回的是一個QQuickTextDocument類型的變量,而這個類型是可以用QSyntaxHighlighter實現語法高亮的(QSyntaxHighlighter的作用類型爲QTextDocument,我們可以通過QQuickTextDocument::textDocument() 函數得到) -
Qt語法高亮官方使用示例:
Syntax Highlighter Example
示例工程源碼地址
我是直接用的官方示例的源碼,只是重寫了setDocument這個函數 -
Highlighter.cpp
Highlighter::Highlighter(QQuickTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.setForeground(Qt::darkBlue);
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns;
keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
<< "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
<< "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
<< "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
<< "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
<< "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
<< "\\bvoid\\b" << "\\bvolatile\\b" << "\\bbool\\b"
<< "\\bfunction\\b" << "\\bend\\b";
foreach (const QString &pattern, keywordPatterns) {
rule.pattern = QRegularExpression(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
//! [0] //! [1]
}
//! [1]
//! [2]
classFormat.setFontWeight(QFont::Bold);
classFormat.setForeground(Qt::darkMagenta);
rule.pattern = QRegularExpression("\\bQ[A-Za-z]+\\b");
rule.format = classFormat;
highlightingRules.append(rule);
//! [2]
//! [3]
singleLineCommentFormat.setForeground(Qt::red);
rule.pattern = QRegularExpression("//[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(Qt::red);
//! [3]
//! [4]
quotationFormat.setForeground(Qt::darkGreen);
rule.pattern = QRegularExpression("\".*\"");
rule.format = quotationFormat;
highlightingRules.append(rule);
rule.pattern = QRegularExpression("\'.*\'");
rule.format = quotationFormat;
highlightingRules.append(rule);
//! [4]
//! [5]
functionFormat.setFontItalic(true);
functionFormat.setForeground(Qt::blue);
rule.pattern = QRegularExpression("\\b[A-Za-z0-9_]+(?=\\()");
rule.format = functionFormat;
highlightingRules.append(rule);
//! [5]
//! [6]
commentStartExpression = QRegularExpression("/\\*");
commentEndExpression = QRegularExpression("\\*/");
}
//! [6]
//! [7]
void Highlighter::highlightBlock(const QString &text)
{
qDebug("%s", __FUNCTION__);
foreach (const HighlightingRule &rule, highlightingRules) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
//! [7] //! [8]
setCurrentBlockState(0);
//! [8]
//! [9]
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = text.indexOf(commentStartExpression);
//! [9] //! [10]
while (startIndex >= 0) {
//! [10] //! [11]
QRegularExpressionMatch match = commentEndExpression.match(text, startIndex);
int endIndex = match.capturedStart();
int commentLength = 0;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ match.capturedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);
}
}
//! [11]
//上面都是示例源碼,我只寫了下面這個函數
void Highlighter::setDocument(QQuickTextDocument *pDoc)
{
QSyntaxHighlighter::setDocument(pDoc->textDocument());
}
- 在main.cpp 中註冊Highlighter類:
qmlRegisterType<Highlighter>("qml.highlighter", 1, 0, "Highlighter");
- 在qml文件中創建Highlighter, 在TextEdit的Component.onCompleted函數中完成setDocument動作:
import qml.highlighter 1.0
Rectangle{
TextEdit{
Layout.fillHeight: true
Layout.fillWidth: true
id:editor
cursorVisible: true
selectByMouse: true
Component.onCompleted: {
hilighter.setDocument(editor.textDocument);
}
}
Highlighter{
id:hilighter
}
}