Important: Please read the Qt Code of Conduct -

Issues with QSyntaxHighlighter (Or Regular Expressions?)

  • Well, so i've made my syntax highlighter with the regular expressions from the Qt site. (Did i say regular expressions don't want to be my friends?)

    Whatever, it works fine. But i keep having those issues:


    As you can see, single line comment works fine. (I haven't done multi-line. Seems a big more challenging.) But whatever, it has some "issues". (Which are strange!)

    For example, i have some keywords like goto, break, new, they show fine, in the single line comment color. But what about if? It's showing on blue! Something is wrong there.

    @ QStringList m_keywordList;
    m_keywordList << "\bgoto\b" << "\bbreak\b" << "\bnew\b" << "\bif\b";
    foreach(const QString &m_keyword, m_keywordList) {
    m_highlightData.insert(m_keyword, m_keywordFormat);

    Second issue, on #define. Yes, works fine. If you see the last quote after Hello, it's on the quotation color instead of the green text.

    @ QStringList m_preprocessorList;
    m_preprocessorList << "#define";
    foreach(const QString &m_preprocessor, m_preprocessorList) {
    m_highlightData.insert("" + m_preprocessor + "[^\n]*\b", m_preprocessorFormat);

    Third issue. Here, we're looking at quotation format. Displays fine, except for the numbers. Different color.

    @m_highlightData.insert("[0-9]", m_numberFormat);
    m_highlightData.insert("".*"", m_quotesFormat);@

    Did i mention regular expressions doesn't like me? Well. What's the issue? I suppose the regular expressions, but why?

  • The first issue was because of the \n, it should be \n.
    The second issue was because of the \n, it should be \n and the \b, doesn't have to be there. (Though it has the same issue as the quotation, but only with keywords)

Log in to reply