<div dir="ltr"><div dir="ltr">On Mon, Jan 29, 2024 at 1:43 AM Maurizio Berti <<a href="mailto:maurizio.berti@gmail.com">maurizio.berti@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>All this is expected, and it's also explained in the documentation of <a href="https://doc.qt.io/qt-6/qtextcursor.html#charFormat" target="_blank">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" target="_blank">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.</div></div></blockquote><div><br></div><div>Maurizio,</div><div><br></div><div>Thank you for the detailed reply and explanation. It's very clear and the results work like a charm. Thank you!</div><div><br></div><div>Best,</div><div>Ovid </div></div></div>