Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Checking formatting of selected text in QTextEdit - PySide6
Forum Updated to NodeBB v4.3 + New Features

Checking formatting of selected text in QTextEdit - PySide6

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tIk90wT
    wrote on 2 Dec 2021, 19:45 last edited by tIk90wT 12 Mar 2021, 17:30
    #1

    I' m working on a rich text editor and came across a small problem which I kind of solved with what seems like a "hack".

    So I wanted to check if the selected text or the text in cursor position has any formatting, bold, italics etc and check the corresponding button that does the corresponding job but it gets weird.

    if I do something like

                           if self.ed.fontItalic() is True:
                                self.italic.setChecked(True)
                            else:
                                self.italic.setChecked(False)
                
                            if self.ed.fontUnderline() is True:
                                self.uline.setChecked(True)
                            else:
                                self.uline.setChecked(False)
    

    where "self.italic" and "self.uline" are checkable QPushButtons;

    Only the format under the cursor gets checked, but not both buttons for italic and underlined, ie, if the selected text has bot formatting.

    Basically, it ignores the selected text and looks for formatting of text under cursor.

    So I I'm currently parsing the html to see if it has formatting;

       def check_formatting(self):
    
            if self.ed.textCursor().hasSelection():
                print(self.ed.textCursor().selection().toHtml())
                if "font-style:italic;" in self.ed.textCursor().selection().toHtml():
                    self.italic.setChecked(True)
                else:
                    self.italic.setChecked(False)
    
                if "text-decoration: underline;" in self.ed.textCursor().selection().toHtml():
                    self.uline.setChecked(True)
                else:
                    self.uline.setChecked(False)
    
            else:
                if self.ed.fontItalic() is True:
                    self.italic.setChecked(True)
                else:
                    self.italic.setChecked(False)
    
                if self.ed.fontUnderline() is True:
                    self.uline.setChecked(True)
                else:
                    self.uline.setChecked(False)
    
    

    and code for formatting selected text or text under cursor on button click is as follows

        def set_italic(self):
                    if self.italic.isChecked():
                        self.ed.setFontItalic(True)
                        if self.ed.textCursor().hasSelection():
                            if self.ed.fontItalic() != QTextFormat.FontItalic:
                                self.ed.setFontItalic(True)
                            else:
                                self.ed.setFontItalic(False)
                    else:
                        self.ed.setFontItalic(False)
    
    

    All of this works nearly as intended but it all feels super hacky. And with the html parsing, obviously, if the user enters "font-style:italic;" in the text edit and selects the entered text, button for italic gets checked.

    I've been looking through a lot of stuff on the internet but none of them helps me achieve as much as the above code does.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Dec 2021, 19:59 last edited by
      #2

      Hi,

      @tIk90wT said in Checking formatting of selected text in QTextEdit - PySide6:

      if self.ed.fontItalic() is True:
      self.italic.setChecked(True)
      else:
      self.italic.setChecked(False)

      That can be simplified:

      self.italic.setChecked(self.ed.fontItalic())
      

      Just to be sure I understand correctly, if you have your cursor on a bit of text that has both italic and underline, only italic is true ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply 2 Dec 2021, 20:24
      1
      • S SGaist
        2 Dec 2021, 19:59

        Hi,

        @tIk90wT said in Checking formatting of selected text in QTextEdit - PySide6:

        if self.ed.fontItalic() is True:
        self.italic.setChecked(True)
        else:
        self.italic.setChecked(False)

        That can be simplified:

        self.italic.setChecked(self.ed.fontItalic())
        

        Just to be sure I understand correctly, if you have your cursor on a bit of text that has both italic and underline, only italic is true ?

        T Offline
        T Offline
        tIk90wT
        wrote on 2 Dec 2021, 20:24 last edited by tIk90wT 12 Feb 2021, 20:36
        #3

        @SGaist If I use

        if self.ed.fontItalic() is True:
                                    self.italic.setChecked(True)
                                else:
                                    self.italic.setChecked(False)
                    
                                if self.ed.fontUnderline() is True:
                                    self.uline.setChecked(True)
                                else:
                                    self.uline.setChecked(False)
        

        and select a bunch of text that has italics, and bold it ignores what's in the selection and gets the format right from the cursor position.

        Like, if text at cursor position has italic only, it checks italic, or has italics and underline, it checks both. If the selection has both italic, underline etc but the text at cursor position has no format, nothing gets checked.

        both checked.png

        not checked.png

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 Dec 2021, 20:50 last edited by
          #4

          Ok, i understand better. In your case you should go through all the blocks included in your selection and then adjust the buttons based on the format of these blocks.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply 3 Dec 2021, 16:15
          1
          • S SGaist
            2 Dec 2021, 20:50

            Ok, i understand better. In your case you should go through all the blocks included in your selection and then adjust the buttons based on the format of these blocks.

            T Offline
            T Offline
            tIk90wT
            wrote on 3 Dec 2021, 16:15 last edited by
            #5

            @SGaist "you should go through all the blocks", I don't really understand this part, if by selection you mean, QTextEdit.textCursor.selection() ..?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 3 Dec 2021, 19:29 last edited by
              #6

              Yes, you would need to go from the start to the end of the selection and get the format of everything in between.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              T 1 Reply Last reply 3 Dec 2021, 20:00
              1
              • S SGaist
                3 Dec 2021, 19:29

                Yes, you would need to go from the start to the end of the selection and get the format of everything in between.

                T Offline
                T Offline
                tIk90wT
                wrote on 3 Dec 2021, 20:00 last edited by
                #7

                @SGaist Isn't selection() QTextDocumentFragment which cannot be iterated?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 3 Dec 2021, 20:04 last edited by
                  #8

                  I was thinking about selectionStart and selectionEnd.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  T 1 Reply Last reply 3 Dec 2021, 20:54
                  1
                  • S SGaist
                    3 Dec 2021, 20:04

                    I was thinking about selectionStart and selectionEnd.

                    T Offline
                    T Offline
                    tIk90wT
                    wrote on 3 Dec 2021, 20:54 last edited by tIk90wT 12 Mar 2021, 20:57
                    #9

                    @SGaist I came up with the following and it works but I'm not sure if it's exactly what you were talking about...

                    self.editor.cursorPositionChanged.connect(self.check_formatting)
                    
                    
                    def check_formatting(self):
                                    if self.ed.textCursor().hasSelection():
                                        cur = self.ed.textCursor()
                                        cur.setPosition(self.ed.textCursor().selectionStart())
                                        cur.setPosition(self.ed.textCursor().selectionEnd(), QTextCursor.KeepAnchor)
                            
                                        ss = cur.selectionStart()
                                        se = cur.selectionEnd()
                                        while ss < se:
                                            cur.setPosition(ss)
                                            if cur.charFormat().fontItalic():
                                                self.italic.setChecked(True)
                                            if cur.charFormat().fontUnderline():
                                                self.uline.setChecked(True)
                                            if cur.charFormat().fontWeight() == QFont.Bold:
                                                self.bold.setChecked(True)
                                            if cur.charFormat().background().style() != Qt.BrushStyle.NoBrush:
                                                self.higl.setChecked(True)
                                            ss += 1
                                    else:
                                        cur = self.ed.textCursor()
                                        self.italic.setChecked(cur.charFormat().fontItalic())
                                        self.uline.setChecked(cur.charFormat().fontUnderline())
                                        if cur.charFormat().fontWeight() == QFont.Bold:
                                            self.bold.setChecked(True)
                                        else:
                                            self.bold.setChecked(False)
                                        if cur.charFormat().background().style() != Qt.BrushStyle.NoBrush:
                                            self.higl.setChecked(True)
                                        else:
                                            self.higl.setChecked(False)
                            
                    
                    

                    PS: Big thanks for helping me reach this point. I'm a complete noob when it come to all the text processing stuffs.

                    S 1 Reply Last reply 3 Dec 2021, 21:03
                    0
                    • T tIk90wT
                      3 Dec 2021, 20:54

                      @SGaist I came up with the following and it works but I'm not sure if it's exactly what you were talking about...

                      self.editor.cursorPositionChanged.connect(self.check_formatting)
                      
                      
                      def check_formatting(self):
                                      if self.ed.textCursor().hasSelection():
                                          cur = self.ed.textCursor()
                                          cur.setPosition(self.ed.textCursor().selectionStart())
                                          cur.setPosition(self.ed.textCursor().selectionEnd(), QTextCursor.KeepAnchor)
                              
                                          ss = cur.selectionStart()
                                          se = cur.selectionEnd()
                                          while ss < se:
                                              cur.setPosition(ss)
                                              if cur.charFormat().fontItalic():
                                                  self.italic.setChecked(True)
                                              if cur.charFormat().fontUnderline():
                                                  self.uline.setChecked(True)
                                              if cur.charFormat().fontWeight() == QFont.Bold:
                                                  self.bold.setChecked(True)
                                              if cur.charFormat().background().style() != Qt.BrushStyle.NoBrush:
                                                  self.higl.setChecked(True)
                                              ss += 1
                                      else:
                                          cur = self.ed.textCursor()
                                          self.italic.setChecked(cur.charFormat().fontItalic())
                                          self.uline.setChecked(cur.charFormat().fontUnderline())
                                          if cur.charFormat().fontWeight() == QFont.Bold:
                                              self.bold.setChecked(True)
                                          else:
                                              self.bold.setChecked(False)
                                          if cur.charFormat().background().style() != Qt.BrushStyle.NoBrush:
                                              self.higl.setChecked(True)
                                          else:
                                              self.higl.setChecked(False)
                              
                      
                      

                      PS: Big thanks for helping me reach this point. I'm a complete noob when it come to all the text processing stuffs.

                      S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 3 Dec 2021, 21:03 last edited by
                      #10

                      @tIk90wT said in Checking formatting of selected text in QTextEdit - PySide6:

                      cur.setPosition(self.ed.textCursor().selectionStart())
                      cur.setPosition(self.ed.textCursor().selectionEnd(), QTextCursor.KeepAnchor)

                      I don't think you need that since the selection is already there.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      T 1 Reply Last reply 3 Dec 2021, 21:07
                      1
                      • S SGaist
                        3 Dec 2021, 21:03

                        @tIk90wT said in Checking formatting of selected text in QTextEdit - PySide6:

                        cur.setPosition(self.ed.textCursor().selectionStart())
                        cur.setPosition(self.ed.textCursor().selectionEnd(), QTextCursor.KeepAnchor)

                        I don't think you need that since the selection is already there.

                        T Offline
                        T Offline
                        tIk90wT
                        wrote on 3 Dec 2021, 21:07 last edited by
                        #11

                        @SGaist Noted. Thanks again.

                        1 Reply Last reply
                        0

                        1/11

                        2 Dec 2021, 19:45

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved