<div dir="ltr"><div dir="ltr">On Fri, Jan 26, 2024 at 12:15 PM Florian Bruhin <<a href="mailto:me@the-compiler.org">me@the-compiler.org</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">I know almost nothing about QTextEditor, so take this with a grain of<br>
salt. If this isn't it, a self-contained runnable example to play around<br>
with would really help: <a href="http://www.sscce.org/" rel="noreferrer" target="_blank">http://www.sscce.org/</a><br>
<br>
> if self.textEditor.currentCharFormat().fontWeight() != QFont.Weight.Bold:<br>
<br>
The docs for QTextEditor::currentCharFormat() say:<br>
<br>
Returns the char format that is used when inserting new text.<br>
<br>
So the behaviour you describe makes sense to me - if I change some<br>
selected text to be bold, I don't want all newly typed text to be bold<br>
too.<br>
<br>
It sounds to me like you might be looking for something like<br>
self.textEditor.textCursor().charFormat() instead, i.e. check if the<br>
text in front of the cursor is bold?<br>
<br>
For what it's worth, the quickcontrols texteditor example in Qt does<br>
something similar, though it looks like it uses a different way of<br>
checking for bold-ness:<br>
<br>
<a href="https://code.qt.io/cgit/qt/qtdeclarative.git/tree/examples/quickcontrols/texteditor/documenthandler.cpp?h=6.6#n279" rel="noreferrer" target="_blank">https://code.qt.io/cgit/qt/qtdeclarative.git/tree/examples/quickcontrols/texteditor/documenthandler.cpp?h=6.6#n279</a><br>
<br></blockquote><div><br></div><div>Hi Florian,</div><div><br>Thanks for the response. Still not sure how to get the behavior I'm looking for, but I took your advice and created a sscce that displays the behavior. You can type in some text and either click the "B" button on the toolbar or hit "ctrl-b". Some debugging output is emitted to the console.</div><div><br></div>import sys<br>from PyQt6.QtWidgets import (<br> QTextEdit,<br> QToolButton,<br> QApplication,<br> QMainWindow,<br> QToolBar,<br>)<br>from PyQt6.QtGui import (<br> QFont,<br> QShortcut,<br> QKeySequence,<br> QTextCharFormat<br>)<br><br>class OvidFont:<br> def __init__(self, ovid) -> None:<br> self.textEditor = ovid.textEditor<br><br> # This should be able to toggle the bold text selected, but it doesn't seem</div><div class="gmail_quote"> # to do so<br> def setBoldText(self):<br> fmt = QTextCharFormat()<br> print(f"charFormat: {self.textEditor.textCursor().charFormat()}")<br> weight = self.textEditor.currentCharFormat().fontWeight()<br> print(f"weight: {weight} QFont.Weight.Bold: {QFont.Weight.Bold}")<br> if self.textEditor.currentCharFormat().fontWeight() == QFont.Weight.Bold:<br> print(" setting normal")<br> fmt.setFontWeight(QFont.Weight.Normal)<br> else:<br> print(" setting bold")<br> fmt.setFontWeight(QFont.Weight.Bold)<br> self.textEditor.textCursor().setCharFormat(fmt)<br><br>class Ovid(QMainWindow):<br> def __init__(self):<br> super().__init__()<br> self.initUI()<br><br> def initUI(self):<br> self.setWindowTitle("Ovid")<br> self.setGeometry(100, 100, 1200, 800)<br> # Create the text editor area<br> self.textEditor = QTextEdit()<br> self.setCentralWidget(self.textEditor)<br> self.fonts = OvidFont(self)<br><br> # Create Toolbar<br> self.toolbar = QToolBar("Main Toolbar")<br> self.addToolBar(self.toolbar)<br><br> # Add actions for text formatting<br> bold_button = QToolButton()<br> bold_button.setText("B")<br> bold_button.setFont(QFont("Arial", 16, QFont.Weight.Bold))<br> bold_button.setToolTip("Bold")<br> bold_button.clicked.connect(self.fonts.setBoldText)<br> self.toolbar.addWidget(bold_button)<br><br> # Shortcuts for text formatting<br> QShortcut(QKeySequence("Ctrl+B"), self, self.fonts.setBoldText)<br><br>def main():<br> app = QApplication(sys.argv)<br> ex = Ovid()<br> ex.show()<br> sys.exit(app.exec())<br><br><br>if __name__ == "__main__":<br> main()<br><div> </div></div></div>