Checking formatting of selected text in QTextEdit - PySide6
-
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 ?
-
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 ?
@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.
-
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.
-
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.
-
Yes, you would need to go from the start to the end of the selection and get the format of everything in between.
-
Yes, you would need to go from the start to the end of the selection and get the format of everything in between.
-
I was thinking about selectionStart and selectionEnd.
-
I was thinking about selectionStart and selectionEnd.
@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.
-
@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.
@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.
-
@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.