QRegExp编译时警告

时间:2016-03-21 00:33:14

标签: c++ qt qregexp

我正在开发一些Qt应用,其主窗口包含QPlainTextEdit子类日志窗口,用于输出事件。我有三种类型的消息:

  1. 信息消息,表示以QString substring
  2. 开头的[INFO]
  3. 警告消息,表示以QString substring
  4. 开头的[WARNING]
  5. 错误消息,表示以QString substring
  6. 开头的[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

1 个答案:

答案 0 :(得分:3)

明星(*)没有被转义。移除\,或者如果您需要\,则应将其转义并写双 \\\)。