<div dir="ltr">Hi all,<div><br></div><div>Google and ChatGPT and Copilot have failed me! </div><div><br></div><div>I'm writing a custom word processor as a hobby project. I'm using Python and PyQt6.<br><br>I wrote the following. The intent is that if I select some text and apply Bold formatting (for example, by hitting "ctrl-b"), it will toggle the formatting. Specifically, it should remove the bold formatting if all of the selected text is bold. Otherwise, it will apply the bold formatting.<br><br><font face="monospace">    class OvidFont:<br>        def __init__(self, ovid) -> None:<br>            self.textEditor = ovid.textEditor</font></div><div><font face="monospace"><br>        def setBoldText(self) -> None:<br>            fmt = QTextCharFormat()</font></div><div><font face="monospace">            if self.textEditor.currentCharFormat().fontWeight() != QFont.Weight.Bold:</font></div><div><font face="monospace">                print("    setting bold")   # for debugging</font></div><div><font face="monospace">                fmt.setFontWeight(QFont.Weight.Bold)</font></div><div><font face="monospace">            else:</font></div><div><font face="monospace">                print("    setting normal") # for debugging</font></div><div><font face="monospace">                fmt.setFontWeight(QFont.Weight.Normal)</font></div><div><font face="monospace">            self.textEditor.textCursor().mergeCharFormat(fmt)</font></div><div><br>However, it won't remove the bold formatting.<br><br>For example, in the sentence "this is a test", if I select "is a" and hit ctrl-b, I get "this **is a** test", with the "is a" properly bold. However, with the selection in place, it still remains bold if I hit "ctrl-b". If I deselect either the first or last character, the toggling of bold works as expected. (I've tried reversing the if/else logic, but that fails, too).<br><br>What am I missing?<br></div><div><br></div><div>Cheers,</div><div>Ovid</div></div>