Python Forum
[PyQt] Determining the format attributes on text in a QTextEdit object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Determining the format attributes on text in a QTextEdit object
#1
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.

    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..
Reply


Messages In This Thread
Determining the format attributes on text in a QTextEdit object - by DrakeSoft - Apr-17-2022, 02:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] AttributeError: 'NoneType' object has no attribute 'text' speedev 9 11,606 Sep-25-2021, 06:14 PM
Last Post: Axel_Erfurt
  [Tkinter] Glow text of clickable object on hover with transition andy 6 6,255 May-11-2021, 07:39 AM
Last Post: andy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020