我正在开发一些Qt
应用,其主窗口包含QPlainTextEdit
子类日志窗口,用于输出事件。我有三种类型的消息:
QString
substring [INFO]
QString
substring [WARNING]
QString
substring [ERROR]
醇>
现在,我想用QSyntaxHighlighter
类实现的是根据类型更改这些消息的颜色( INFO 类型 - Qt::DarkBlue
颜色,警告类型 - Qt::DarkYellow
颜色,错误类型 - Qt::DarkRed
颜色)这里是代码块,应该完成任务:
void UeLogWindowTextHighlighter::ueSetupRules()
{
UeHighlightRule* ueRuleInfo=new UeHighlightRule(this);
UeHighlightRule* ueRuleWarning=new UeHighlightRule(this);
UeHighlightRule* ueRuleError=new UeHighlightRule(this);
this->ueInfoStartExpression()->setPattern("^[INFO].\*"); // FIRST WARNING
this->ueWarningStartExpression()->setPattern("^[WARNING].\*"); // SECOND WARNING
this->ueErrorStartExpression()->setPattern("^[ERROR].\*"); // THIRD WARNING
this->ueInfoExpressionCharFormat()->setForeground(Qt::darkGray);
this->ueWarningExpressionCharFormat()->setForeground(Qt::darkYellow);
this->ueErrorExpressionCharFormat()->setForeground(Qt::darkRed);
ueRuleInfo->ueSetPattern(this->ueInfoStartExpression());
ueRuleInfo->ueSetTextCharFormat(this->ueInfoExpressionCharFormat());
this->ueHighlightRules()->append(ueRuleInfo);
ueRuleWarning->ueSetPattern(this->ueWarningStartExpression());
ueRuleWarning->ueSetTextCharFormat(this->ueWarningExpressionCharFormat());
this->ueHighlightRules()->append(ueRuleWarning);
ueRuleError->ueSetPattern(this->ueErrorStartExpression());
ueRuleError->ueSetTextCharFormat(this->ueErrorExpressionCharFormat());
this->ueHighlightRules()->append(ueRuleError);
} // ueSetupRules
但是,当我编译项目时,我会收到警告:
../../../gui/uelogwindowtexthighlighter.cpp: In member function 'void UeLogWindowTextHighlighter::ueSetupRules()': ../../../gui/uelogwindowtexthighlighter.cpp:58:47: warning: unknown escape sequence: '\*' [enabled by default]
this->ueInfoStartExpression()->setPattern("^[INFO].\*");
^ ../../../gui/uelogwindowtexthighlighter.cpp:59:50: warning: unknown escape sequence: '\*' [enabled by default]
this->ueWarningStartExpression()->setPattern("^[WARNING].\*");
^ ../../../gui/uelogwindowtexthighlighter.cpp:60:48: warning: unknown escape sequence: '\*' [enabled by default]
this->ueErrorStartExpression()->setPattern("^[ERROR].\*");
^
因此消息没有着色(这是我的怀疑)。 我的正则表达式有什么问题?我关注this question and answer on SO。
答案 0 :(得分:3)
明星(*
)没有被转义。移除\
,或者如果您需要\
,则应将其转义并写双 \
(\\
)。