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?