[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: PyQt6 Not toggling "bold" text.
From:       Ovid <curtis.poe () gmail ! com>
Date:       2024-01-29 7:02:51
Message-ID: CA+M4CHu6+sD0G6wBqn2j+LD=1Xy7hzHekVv9w-_OB+cK9o6jUA () mail ! gmail ! com
[Download RAW message or body]

On Mon, Jan 29, 2024 at 1:43 AM Maurizio Berti <maurizio.berti@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

[Attachment #3 (text/html)]

<div dir="ltr"><div dir="ltr">On Mon, Jan 29, 2024 at 1:43 AM Maurizio Berti &lt;<a \
href="mailto:maurizio.berti@gmail.com">maurizio.berti@gmail.com</a>&gt; \
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&#39;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&#39;s often like the above.<br></div><div><br></div><div>Your approach won&#39;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&#39;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&#39;s very clear and the results work \
like a charm. Thank you!</div><div><br></div><div>Best,</div><div>Ovid  \
</div></div></div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic