Skip to content

Instantly share code, notes, and snippets.

@Ovid
Created January 26, 2024 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ovid/65936985c6838c0220620cf40ba935fa to your computer and use it in GitHub Desktop.
Save Ovid/65936985c6838c0220620cf40ba935fa to your computer and use it in GitHub Desktop.
Bold Toggling Bug
"""
This is a minimum test case for a bug in some code I'm writing. It's for a custom text editor (hobby
project).
The intent is that if I select some text and apply Bold formatting (for example, by hitting "ctrl-b"),
it will toggle the formatting. Specifically, it should remove the bold formatting if all of the selected
text is bold. Otherwise, it will apply the bold formatting.
However, it won't remove the bold formatting.
For example, in the sentence "this is a test", if I select "is a" and apply the bold formatting, I get
"this is a test", with the "is a" properly bold. However, with the selection in place, it still remains
bold if I hit "ctrl-b". If I deselect either the first or last character, the toggling of bold works as
expected. (I've tried reversing the if/else logic, but that fails, too).
In the example below, you can also click the "B" button on the menu bar to apply bold.
"""
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
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)
cursor = self.textEditor.textCursor()
cursor.setCharFormat(fmt)
self.textEditor.setTextCursor(cursor)
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment