<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">Hi,</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">Other than Google and AI, searching code in github is also an alternative: searching for mergeCharFormat yields</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><a href="https://github.com/kovidgoyal/calibre/blob/07f988b109627bdbb88ab4b43ccfa6956276c682/src/calibre/gui2/comments_editor.py#L465">https://github.com/kovidgoyal/calibre/blob/07f988b109627bdbb88ab4b43ccfa6956276c682/src/calibre/gui2/comments_editor.py#L465</a></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">From the look of it, your code is missing `self.textEditor.setTextCursor(...)` because `textCursor()` returns a copy.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jan 26, 2024 at 5:50 PM Ovid <<a href="mailto:curtis.poe@gmail.com">curtis.poe@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><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>
</blockquote></div>