30 : QSyntaxHighlighter(parent)
32 HighlightingRule rule;
34 keywordFormat.setForeground(Qt::darkBlue);
35 keywordFormat.setFontWeight(QFont::Bold);
36 QStringList keywordPatterns;
37 keywordPatterns <<
"\\bfor\\b" <<
"\\bwhile\\b" <<
"\\bif\\b" <<
"\\bchar\\b"
38 <<
"\\bclass\\b" <<
"\\bconst\\b" <<
"\\breturn\\b"
39 <<
"\\bdouble\\b" <<
"\\benum\\b" <<
"\\bexplicit\\b"
40 <<
"\\bfriend\\b" <<
"\\binline\\b" <<
"\\bint\\b"
41 <<
"\\blong\\b" <<
"\\bnamespace\\b" <<
"\\boperator\\b"
42 <<
"\\bprivate\\b" <<
"\\bprotected\\b" <<
"\\bpublic\\b"
43 <<
"\\bshort\\b" <<
"\\bsignals\\b" <<
"\\bsigned\\b"
44 <<
"\\bslots\\b" <<
"\\bstatic\\b" <<
"\\bstruct\\b"
45 <<
"\\btemplate\\b" <<
"\\btypedef\\b" <<
"\\btypename\\b"
46 <<
"\\bunion\\b" <<
"\\bunsigned\\b" <<
"\\bvirtual\\b"
47 <<
"\\bvoid\\b" <<
"\\bvolatile\\b";
48 foreach (
const QString& pattern, keywordPatterns)
50 rule.pattern = QRegExp(pattern);
51 rule.format = keywordFormat;
52 highlightingRules.append(rule);
55 classFormat.setFontWeight(QFont::Bold);
56 classFormat.setForeground(Qt::darkMagenta);
57 rule.pattern = QRegExp(
"\\bQ[A-Za-z]+\\b");
58 rule.format = classFormat;
59 highlightingRules.append(rule);
61 singleLineCommentFormat.setForeground(
Qt::red);
62 rule.pattern = QRegExp(
"//[^\n]*");
63 rule.format = singleLineCommentFormat;
64 highlightingRules.append(rule);
66 multiLineCommentFormat.setForeground(
Qt::red);
68 quotationFormat.setForeground(Qt::darkGreen);
69 rule.pattern = QRegExp(
"\".*\"");
70 rule.format = quotationFormat;
71 highlightingRules.append(rule);
73 functionFormat.setFontItalic(
true);
74 functionFormat.setForeground(Qt::blue);
75 rule.pattern = QRegExp(
"\\b[A-Za-z0-9_]+(?=\\()");
76 rule.format = functionFormat;
77 highlightingRules.append(rule);
79 commentStartExpression = QRegExp(
"/\\*");
80 commentEndExpression = QRegExp(
"\\*/");
85 foreach (
const HighlightingRule& rule, highlightingRules)
87 QRegExp expression(rule.pattern);
88 int index = expression.indexIn(text);
92 int length = expression.matchedLength();
93 setFormat(
index, length, rule.format);
94 index = expression.indexIn(text,
index + length);
97 setCurrentBlockState(0);
101 if (previousBlockState() != 1)
103 startIndex = commentStartExpression.indexIn(text);
106 while (startIndex >= 0)
108 int endIndex = commentEndExpression.indexIn(text, startIndex);
113 setCurrentBlockState(1);
114 commentLength = text.length() - startIndex;
118 commentLength = endIndex - startIndex
119 + commentEndExpression.matchedLength();
122 setFormat(startIndex, commentLength, multiLineCommentFormat);
123 startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);