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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on 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
    1
    • SGaistS SGaist

      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 last edited by tIk90wT
      #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
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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
        1
        • SGaistS SGaist

          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 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
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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
            1
            • SGaistS SGaist

              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 last edited by
              #7

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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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
                1
                • SGaistS SGaist

                  I was thinking about selectionStart and selectionEnd.

                  T Offline
                  T Offline
                  tIk90wT
                  wrote on last edited by tIk90wT
                  #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.

                  SGaistS 1 Reply Last reply
                  0
                  • T tIk90wT

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

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 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
                    1
                    • SGaistS SGaist

                      @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 last edited by
                      #11

                      @SGaist Noted. Thanks again.

                      1 Reply Last reply
                      0

                      • Login

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