<div dir="ltr"><div>All this is expected, and it's also explained in the documentation of <a href="https://doc.qt.io/qt-6/qtextcursor.html#charFormat">QTextCursor::charFormat()</a>:</div><div><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>Returns the format of the character immediately before the cursor position(). If the cursor is positioned at the beginning of a text block that is not empty then the format of the character immediately after the cursor is returned.</div></blockquote><div><br></div><div>This is also the common behavior for basic rich text editors (including most web based ones, like the GMail composer): when you move at the *left* of a different character format and start typing, you will always use the format of the *previous* character. Behavior may change depending on the editors, but when using keyboard navigation it's often like the above.<br></div><div><br></div><div>Your approach won't work specifically when there is selected text, and the cursor (the anchor in QTextCursor context) is at the beginning of the selection, <b>but</b> the cursor is not at the beginning of the block.<br></div><div><br></div><div>The solution is then to always check if the cursor is at the beginning of the selection (but not of a block) and eventually move it to the end of the selection.<br></div><div>Then just get the char format and use the opposite weight with <span style="font-family:monospace">QTextEdit.setFontWeight()</span> (no need to use the cursor).<br><br></div><div>A couple of hours ago, a <a href="https://stackoverflow.com/a/77895460/2001654">solution was posted on SO</a>, and was eventually fixed after some suggestions (for future reference, always include relevant examples in the question body, not as external links).</div><div><br></div><div>You can also completely ignore ChatGPT and similar Generative AIs for such matters, as they are completely unreliable. Qt (and even more PyQt/PySide) has a relatively niche following, meaning that AI models have very little statistical data to rely upon; add that to the fact that GAI have absolutely no concept of context abstraction and their meaning, and you'll see how useless it is in such matters.<br><br></div><div>Best regards,<br></div><div>MaurizioB<br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno ven 26 gen 2024 alle ore 14:12 Ovid <<a href="mailto:curtis.poe@gmail.com">curtis.poe@gmail.com</a>> ha scritto:<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"><div dir="ltr">On Fri, Jan 26, 2024 at 2:00 PM Charles <<a href="mailto:peacech@gmail.com" target="_blank">peacech@gmail.com</a>> wrote:<br></div><div class="gmail_quote"><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"><div style="font-family:arial,helvetica,sans-serif;font-size:small">> 
For your code, I hit "crtl-b" twice and it goes bold and stays bold. I 
wonder if maybe there's a bug on the MacOS version? What version of 
PyQt6 do you have? <br></div><div style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div style="font-family:arial,helvetica,sans-serif;font-size:small">Yes, like a normal rich text editor. Windows 10, python 3.12.1 x64, PyQt 6.6.1.<br></div></div><div class="gmail_quote"><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"><div class="gmail_quote"><div></div></div></div></blockquote></div></blockquote><div><br></div><div>Doubly-weird! I just ran your code on both MacOS and Debian (I don't have a Windows box) and I have the bug on both.</div><div><br></div><div>Anyone know where bug reports can be filed? I don't see how from <a href="https://www.riverbankcomputing.com/software/pyqt/" target="_blank">https://www.riverbankcomputing.com/software/pyqt/</a><br><br>Best,</div><div>Ovid </div></div></div>
</blockquote></div><br clear="all"><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div>