PyQt6 Not toggling "bold" text.
Ovid
curtis.poe at gmail.com
Fri Jan 26 11:50:13 GMT 2024
On Fri, Jan 26, 2024 at 12:15 PM Florian Bruhin <me at the-compiler.org> wrote:
> 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
>
>
Hi Florian,
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.
import sys
from PyQt6.QtWidgets import (
QTextEdit,
QToolButton,
QApplication,
QMainWindow,
QToolBar,
)
from PyQt6.QtGui import (
QFont,
QShortcut,
QKeySequence,
QTextCharFormat
)
class OvidFont:
def __init__(self, ovid) -> None:
self.textEditor = ovid.textEditor
# This should be able to toggle the bold text selected, but it doesn't
seem
# to do so
def setBoldText(self):
fmt = QTextCharFormat()
print(f"charFormat: {self.textEditor.textCursor().charFormat()}")
weight = self.textEditor.currentCharFormat().fontWeight()
print(f"weight: {weight} QFont.Weight.Bold: {QFont.Weight.Bold}")
if self.textEditor.currentCharFormat().fontWeight() ==
QFont.Weight.Bold:
print(" setting normal")
fmt.setFontWeight(QFont.Weight.Normal)
else:
print(" setting bold")
fmt.setFontWeight(QFont.Weight.Bold)
self.textEditor.textCursor().setCharFormat(fmt)
class Ovid(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("Ovid")
self.setGeometry(100, 100, 1200, 800)
# Create the text editor area
self.textEditor = QTextEdit()
self.setCentralWidget(self.textEditor)
self.fonts = OvidFont(self)
# Create Toolbar
self.toolbar = QToolBar("Main Toolbar")
self.addToolBar(self.toolbar)
# Add actions for text formatting
bold_button = QToolButton()
bold_button.setText("B")
bold_button.setFont(QFont("Arial", 16, QFont.Weight.Bold))
bold_button.setToolTip("Bold")
bold_button.clicked.connect(self.fonts.setBoldText)
self.toolbar.addWidget(bold_button)
# Shortcuts for text formatting
QShortcut(QKeySequence("Ctrl+B"), self, self.fonts.setBoldText)
def main():
app = QApplication(sys.argv)
ex = Ovid()
ex.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20240126/4aad612e/attachment-0001.htm>
More information about the PyQt
mailing list