Apr-17-2022, 02:01 PM
Hi,
The following function is used to highlight text for a spell checker. If it finds a misspelt word it will underline it in red.
Given a block of text in a QTextEdit object is there some way of determining the format attributes of that text. It is almost the opposite of QSyntaxHighlighter.setFormat() method. I want to be able to iterate through the block and find the underlined words or words highlighted in blue etc..
The following function is used to highlight text for a spell checker. If it finds a misspelt word it will underline it in red.
def highlightBlock(self, text: str) -> None: if not hasattr(self, "speller"): return # The character format of text in a document specifies the visual properties of the text, as well as information about its role in a hypertext document. self.misspelledFormat = QTextCharFormat() self.misspelledFormat.setUnderlineStyle( QTextCharFormat.SpellCheckUnderline) # we can set its visual style self.misspelledFormat.setUnderlineColor(Qt.red) # red and underlined # we iterate the text using the regular expression above which identifies word boundaries for word_object in self.wordRegEx.finditer(text): # we check to see if this is a recognised word if not self.speller.check(word_object.group()): self.setFormat( # if it is not we underline it using the style shown above word_object.start(), # index of first letter of match # index of last letter - index of first letter= length word_object.end() - word_object.start(), self.misspelledFormat, )I am trying to figure out how I might do the opposite. For example:
Given a block of text in a QTextEdit object is there some way of determining the format attributes of that text. It is almost the opposite of QSyntaxHighlighter.setFormat() method. I want to be able to iterate through the block and find the underlined words or words highlighted in blue etc..