PDA

View Full Version : QSyntaxHighlighter for rows inside a table inside a QTextEdit



Saelyth
19th March 2018, 21:21
Hello guys, I fail to find how to make QSyntaxHighlighter works in an HTML table inside a QtextEdit.

I have something like this:

12794

And I am wondering how to hightlight text in the middle column. Specifically rules like:

Highlight the first letter if the text doesn't starts with an upper.
Highlight the entire row if the text doesn't ends in the next symbols: ?!.

I tried the next code for it (Not C++, it's PyQt5) and it got close to what I want to do, but it works for the entire document, doesn't differenciate the rows on the table.
I'm lost on how to tell the program that it should only apply rules for an html Cell on the 2nd column of the table and not the first column.



self.highlighter = Highlighter(self.my_lineedit.document())

class Highlighter(QSyntaxHighlighter):
def __init__(self, parent):
super(Highlighter, self).__init__(parent)
self.sectionFormat = QTextCharFormat()
self.sectionFormat.setForeground(Qt.blue)

def highlightBlock(self, text):
if text.startswith('Test'):
self.setFormat(0, len(text), self.sectionFormat)

Any suggestion is appreciated. Even if the suggestions are for C++ code, it will help me to understand how it works and I'll adapt it to python myself.

Note: I am using python 3.6 + PyQt 5.10.1

wysota
21st March 2018, 08:06
QSyntaxHighlighter is not meant to work with rich text. You have to do your own parsing and coloring by injecting QTextCharFormat objects directly into your document.