PyQt6 Not toggling "bold" text.

Florian Bruhin me at the-compiler.org
Fri Jan 26 11:15:02 GMT 2024


Hey,

I know almost nothing about QTextEditor, so take this with a grain of
salt. If this isn't it, a self-contained runnable example to play around
with would really help: http://www.sscce.org/

> if self.textEditor.currentCharFormat().fontWeight() != QFont.Weight.Bold:

The docs for QTextEditor::currentCharFormat() say:

    Returns the char format that is used when inserting new text.

So the behaviour you describe makes sense to me - if I change some
selected text to be bold, I don't want all newly typed text to be bold
too.

It sounds to me like you might be looking for something like
self.textEditor.textCursor().charFormat() instead, i.e. check if the
text in front of the cursor is bold?

For what it's worth, the quickcontrols texteditor example in Qt does
something similar, though it looks like it uses a different way of
checking for bold-ness:

https://code.qt.io/cgit/qt/qtdeclarative.git/tree/examples/quickcontrols/texteditor/documenthandler.cpp?h=6.6#n279

Florian


More information about the PyQt mailing list