
我正在编写一个自定义文字处理器作为一个业余爱好项目。我正在使用 python 和 pyqt6。
我写了以下内容。目的是,如果我选择一些文本并应用粗体格式(例如,通过点击“ctrl-b”),它将切换格式。具体来说,如果所有选定的文本都是粗体,则应删除粗体格式。否则,它将应用粗体格式。
class OvidFont:
def __init__(self, ovid) -> None:
self.textEditor = ovid.textEditor
def setBoldText(self) -> None:
fmt = QTextCharFormat()
if self.textEditor.currentCharFormat().fontWeight() != QFont.Weight.Bold:
print(" setting bold") # for debugging
fmt.setFontWeight(QFont.Weight.Bold)
else:
print(" setting normal") # for debugging
fmt.setFontWeight(QFont.Weight.Normal)
self.textEditor.textCursor().mergeCharFormat(fmt)但是,它不会删除粗体格式。
例如,在句子“this is a test”中,如果我选择“is a”并应用粗体格式,我会得到“this is a test”,其中“is a” “适当大胆。然而,选择到位后,如果我点击“ctrl-b”,它仍然保持粗体。如果我取消选择第一个或最后一个字符,粗体切换将按预期工作。 (我尝试过反转 if/else 逻辑,但也失败了)。
我错过了什么?
更新:我在 https://gist.github.com/ovid/65936985c6838c0220620cf40ba935fa 添加了一个有效的最小测试用例
1、对ASP内核代码进行DLL封装,从而大大提高了用户的访问速度和安全性;2、采用后台生成HTML网页的格式,使程序访问速度得到进一步的提升;3、用户可发展下级会员并在下级购买商品时获得差额利润;4、全新模板选择功能;5、后台增加磁盘绑定功能;6、后台增加库存查询功能;7、后台增加财务统计功能;8、后台面值类型批量设定;9、后台财务曲线报表显示;10、完善订单功能;11、对所有传输的字符串进行安全
0
setboldtext 函数的问题在于它使用 self.texteditor.currentcharformat().fontweight() 检查粗体状态,这仅反映当前光标位置处字符的格式,而不是整个选定文本的格式。如果您的光标位于所选内容的开头或结尾,它可能无法准确表示整个所选范围的格式。
因此,我使用现有的光标,并根据需要进行调整以检查格式,并在 setfontweight() 上直接应用新的字体粗细。
现在看起来像这样:
更新的代码:
import sys
from PyQt6.QtWidgets import QTextEdit, QToolButton, QApplication, QMainWindow, QToolBar
from PyQt6.QtGui import QFont, QShortcut, QKeySequence, QTextCharFormat, QTextCursor
class OvidFont:
def __init__(self, ovid) -> None:
self.textEditor = ovid.textEditor
def setBoldText(self):
cursor = self.textEditor.textCursor()
# If there's a selection, and the cursor is not at the block start and at the beginning of the selection,
# move the cursor to the end of the selection
if cursor.hasSelection() and not cursor.atBlockStart() and cursor.position() == cursor.selectionStart():
cursor.setPosition(cursor.selectionEnd())
# Check if the text (either selected or where the cursor is) is bold
is_bold = cursor.charFormat().fontWeight() == QFont.Weight.Bold
# Apply the new weight based on the current state
new_weight = QFont.Weight.Normal if is_bold else QFont.Weight.Bold
self.textEditor.setFontWeight(new_weight)
print(f"Bold set to: {'Normal' if is_bold else 'Bold'}")
class Ovid(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("Ovid")
self.setGeometry(100, 100, 200, 200)
self.textEditor = QTextEdit()
self.setCentralWidget(self.textEditor)
self.fonts = OvidFont(self)
self.toolbar = QToolBar("Main Toolbar")
self.addToolBar(self.toolbar)
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)
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()以上就是使用 PyQt6 切换字符格式的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号