Results 1 to 3 of 3

Thread: Highlight with two different syntaxes with QSyntaxHighlighter

  1. #1
    Join Date
    Oct 2009
    Posts
    18
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Highlight with two different syntaxes with QSyntaxHighlighter

    Hello.

    I have a working class that highlights fine right now.
    Straight from the docs
    Qt Code:
    1. void myClass::highlightBlock(const QString &text)
    2. {
    3. int length ;
    4. int index = text.indexOf(this->m_pattern);
    5. while (index >= 0)
    6. {
    7. length = this->m_pattern.matchedLength();
    8. setFormat(index, length, this->m_format);
    9. index = text.indexOf(this->m_pattern, index + length);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    But now I wish to add another pattern and highlight with that. So I wonder how one should do this: Something like this?

    Qt Code:
    1. void myClass::highlightBlock(const QString &text)
    2. {
    3. int index,index2,length;
    4. index = text.indexOf(this->m_pattern);
    5. index2 = text.indexOf(this->m_pattern2);
    6. while (index >= 0 && index2 >=0)
    7. {
    8. if (index2 < index && index2>0=0)
    9. {
    10. length = this->m_pattern2.matchedLength();
    11. setFormat(index, length, this->m_format2);
    12. }
    13. else if (index < index2 && index>=0)
    14. {
    15. length = this->m_pattern.matchedLength();
    16. setFormat(index, length, this->m_format);
    17. }
    18.  
    19. // perhaps some clever check to see that the matches do not overlap
    20.  
    21. index = text.indexOf(this->m_pattern, index + length);
    22. index = text.indexOf(this->m_pattern2, index + length);
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    Will this work?

    And how do you do really do this if one would write an editor with proper syntax highlighting for a language for example c++, how do you separate all the different kind of patterns to highlight with different formats?

  2. #2
    Join Date
    Sep 2009
    Posts
    20
    Thanked 2 Times in 2 Posts

    Default Re: Highlight with two different syntaxes with QSyntaxHighlighter

    Hi,

    Please check syntaxhighlighter example from Qt, It shows how to make
    syntax highlighting for C++. It's not perfect example because highlighting
    doesn't work well, but you will understand how to add many highligh rules.


    Regards

  3. #3
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Highlight with two different syntaxes with QSyntaxHighlighter

    Maybe this will help you! I have pimped the syntaxhighlighter example a bit:

    Now the rules have names, so that you can add/replace a search-highlight rule with varying patterns. The map returns the rules sorted by keys. The order is relevant because for example strings in comments or function calls in comments should not be highlighted..

    Header-File:

    Qt Code:
    1. #ifndef SYNTAXHIGHLIGHTER_H #define SYNTAXHIGHLIGHTER_H
    2. #include <QSyntaxHighlighter>
    3. #include <QTextCharFormat>
    4. QT_BEGIN_NAMESPACE
    5. QT_END_NAMESPACE
    6. class Highlighter : public QSyntaxHighlighter
    7. { Q_OBJECT
    8. public:
    9. Highlighter(QTextDocument *parent = 0);
    10. void SetRule(QString name,QString pattern,QTextCharFormat format);
    11. protected:
    12. void highlightBlock(const QString &text);
    13. struct HighlightingRule
    14. {
    15. HighlightingRule() {}
    16. HighlightingRule(QRegExp _pattern,QTextCharFormat _format) {pattern = _pattern;format = _format;}
    17. QRegExp pattern;
    18. };
    19. QMap<QString,HighlightingRule> highlightingRules;
    20. };
    21. class MultiLineCommentHighlighter : public Highlighter
    22. { Q_OBJECT
    23. public:
    24. MultiLineCommentHighlighter(QTextDocument *parent = 0);
    25. protected:
    26. void highlightBlock(const QString &text);
    27. QRegExp commentStartExpression;
    28. QRegExp commentEndExpression;
    29. QTextCharFormat multiLineCommentFormat;
    30. };
    31. class CppHighlighter : public MultiLineCommentHighlighter
    32. { Q_OBJECT
    33. public:
    34. CppHighlighter(QTextDocument *parent = 0);
    35. };
    36. #endif // SYNTAXHIGHLIGHTER_H
    To copy to clipboard, switch view to plain text mode 
    SyntaxHighlighter.cpp:
    Qt Code:
    1. #include "SyntaxHighlighter.h" #include <QtGui>
    2. /****************************************************************************************
    3.   ****************************************************************************************
    4.   **** Highlighter ***********************************************************************
    5.   ****************************************************************************************
    6.   ****************************************************************************************/
    7. Highlighter::Highlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
    8. {
    9. }
    10. void Highlighter::SetRule(QString name,QString pattern,QTextCharFormat format)
    11. {
    12. if (pattern != "")
    13. highlightingRules.insert(name,HighlightingRule(QRegExp(pattern),format));
    14. else
    15. highlightingRules.remove(name);
    16. rehighlight();
    17. }
    18. void Highlighter::highlightBlock(const QString &text)
    19. {
    20. foreach (const HighlightingRule &rule, highlightingRules) {
    21. QRegExp expression(rule.pattern);
    22. int index = expression.indexIn(text);
    23. while (index >= 0) {
    24. int length = expression.matchedLength();
    25. setFormat(index, length, rule.format);
    26. index = expression.indexIn(text, index + length);
    27. }
    28. }
    29. setCurrentBlockState(0);
    30. }
    31. /****************************************************************************************
    32.   ****************************************************************************************
    33.   **** MultiLineCommentHighlighter *******************************************************
    34.   ****************************************************************************************
    35.   ****************************************************************************************/
    36. MultiLineCommentHighlighter::MultiLineCommentHighlighter(QTextDocument *parent) : Highlighter(parent)
    37. {
    38. multiLineCommentFormat.setForeground(Qt::red);
    39. commentStartExpression = QRegExp("/\\*");
    40. commentEndExpression = QRegExp("\\*/");
    41. }
    42. void MultiLineCommentHighlighter::highlightBlock(const QString &text)
    43. {
    44. Highlighter::highlightBlock(text);
    45. int startIndex = 0;
    46. if (previousBlockState() != 1)
    47. startIndex = commentStartExpression.indexIn(text);
    48. while (startIndex >= 0) {
    49. int endIndex = commentEndExpression.indexIn(text, startIndex);
    50. int commentLength;
    51. if (endIndex == -1) {
    52. setCurrentBlockState(1);
    53. commentLength = text.length() - startIndex;
    54. } else {
    55. commentLength = endIndex - startIndex
    56. + commentEndExpression.matchedLength();
    57. }
    58. setFormat(startIndex, commentLength, multiLineCommentFormat);
    59. startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
    60. }
    61. }
    62. /****************************************************************************************
    63.   ****************************************************************************************
    64.   **** CppHighlighter ********************************************************************
    65.   ****************************************************************************************
    66.   ****************************************************************************************/
    67. CppHighlighter::CppHighlighter(QTextDocument *parent) : MultiLineCommentHighlighter(parent)
    68. {
    69. QTextCharFormat keywordFormat;
    70. QTextCharFormat classFormat;
    71. QTextCharFormat singleLineCommentFormat;
    72. QTextCharFormat quotationFormat;
    73. QTextCharFormat functionFormat;
    74. keywordFormat.setForeground(Qt::darkBlue);
    75. keywordFormat.setFontWeight(QFont::Bold);
    76. QStringList keywordPatterns;
    77. keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
    78. << "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
    79. << "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
    80. << "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
    81. << "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
    82. << "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
    83. << "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
    84. << "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
    85. << "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
    86. << "\\bvoid\\b" << "\\bvolatile\\b";
    87. int i = 0;
    88. foreach (const QString &pattern, keywordPatterns) {
    89. SetRule(QString("00_KeyWord_%1").arg(i),pattern,keywordFormat);
    90. ++i;
    91. }
    92. classFormat.setFontWeight(QFont::Bold);
    93. classFormat.setForeground(Qt::darkMagenta);
    94. SetRule("01_QtClasses","\\bQ[A-Za-z]+\\b",classFormat);
    95. singleLineCommentFormat.setForeground(Qt::red);
    96. SetRule("02_SingleLineComment","//[^\n]*",singleLineCommentFormat);
    97. quotationFormat.setForeground(Qt::darkGreen);
    98. SetRule("03_Quotation","\".*\"",quotationFormat);
    99. functionFormat.setFontItalic(true);
    100. functionFormat.setForeground(Qt::blue);
    101. SetRule("04_Functions","\\b[A-Za-z0-9_]+(?=\\()",functionFormat);
    102. }
    To copy to clipboard, switch view to plain text mode 
    HIH

    Johannes

Similar Threads

  1. QTableView item's Highlight in QCombox
    By litterflybug in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2009, 14:06
  2. QSyntaxHighlighter and faked currentParagraph()
    By mcostalba in forum Qt Programming
    Replies: 4
    Last Post: 11th December 2008, 00:36
  3. Hover and Highlight QTable and QTree Items
    By VireX in forum Qt Programming
    Replies: 41
    Last Post: 18th May 2007, 21:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.