PyQt6 Not toggling "bold" text.

Charles peacech at gmail.com
Fri Jan 26 12:44:43 GMT 2024


It works for me though:

import sys

from PyQt6.QtCore import Qt
from PyQt6.QtGui import QFont, QKeySequence, QShortcut, QTextCharFormat
from PyQt6.QtWidgets import QApplication, QMainWindow, QTextEdit


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        edit = QTextEdit()
        self.setCentralWidget(edit)
        self._edit = edit

        s = QShortcut(QKeySequence(Qt.Modifier.CTRL | Qt.Key.Key_B), self)
        s.setContext(Qt.ShortcutContext.WidgetWithChildrenShortcut)
        s.activated.connect(self.toggleBold)

    def toggleBold(self):
        fmt = QTextCharFormat()
        cur = self._edit.textCursor()
        fmt.setFontWeight(QFont.Weight.Bold if
cur.charFormat().fontWeight() < QFont.Weight.Bold else QFont.Weight.Normal)
        cur.mergeCharFormat(fmt)
        self._edit.setTextCursor(cur)


app = QApplication(sys.argv)
win = MainWindow()
win.show()
app.exec()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20240126/4ddd70d1/attachment.htm>


More information about the PyQt mailing list