PyQt6 Not toggling "bold" text.

Maurizio Berti maurizio.berti at gmail.com
Mon Jan 29 00:42:54 GMT 2024


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.

Best regards,
MaurizioB



Il giorno ven 26 gen 2024 alle ore 14:12 Ovid <curtis.poe at gmail.com> ha
scritto:

> On Fri, Jan 26, 2024 at 2:00 PM Charles <peacech at gmail.com> wrote:
>
>> > 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?
>>
>> Yes, like a normal rich text editor. Windows 10, python 3.12.1 x64, PyQt
>> 6.6.1.
>>
>>>
> 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.
>
> Anyone know where bug reports can be filed? I don't see how from
> https://www.riverbankcomputing.com/software/pyqt/
>
> Best,
> Ovid
>


-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20240129/260f4439/attachment.htm>


More information about the PyQt mailing list