PyQt6 Not toggling "bold" text.

Ovid curtis.poe at gmail.com
Mon Jan 29 07:02:51 GMT 2024


On Mon, Jan 29, 2024 at 1:43 AM Maurizio Berti <maurizio.berti at gmail.com>
wrote:

> All this is expected, and it's also explained in the documentation of
> QTextCursor::charFormat()
> <https://doc.qt.io/qt-6/qtextcursor.html#charFormat>:
>
> 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.
>>
>
> 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.
>
> 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, *but* the cursor is not at the beginning of the block.
>
> 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.
> Then just get the char format and use the opposite weight with
> QTextEdit.setFontWeight() (no need to use the cursor).
>
> A couple of hours ago, a solution was posted on SO
> <https://stackoverflow.com/a/77895460/2001654>, and was eventually fixed
> after some suggestions (for future reference, always include relevant
> examples in the question body, not as external links).
>
> 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.
>

Maurizio,

Thank you for the detailed reply and explanation. It's very clear and the
results work like a charm. Thank you!

Best,
Ovid
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20240129/7e863cc1/attachment.htm>


More information about the PyQt mailing list