在自定义 QTextBrowser 中同步文本选择器和字体

问题描述

我正在尝试在文本编辑器中构建基本功能,当您单击新的文本块时,字体选择器框会自动更新,以便它反映该文本的当前字体。 (下面贴出最少的代码。)

我已成功获得所需的行为。但是,由于未知原因,更新 QFontComboBox 的关键代码行抑制了该组合框在更改文本时实际更新文本字体的能力。

换句话说,似乎我只能有一种方法 - QFontComboBox 可以更新 QTextbrowser 中的字体,或者光标在 QTextbrowser 中的文本可以更新 QFontComboBox,但不能同时更新。我可以通过注释掉一行来恢复设置字体的能力:self.fontBox.setCurrentFont(cursor.charFormat().font())

我的项目中可能涉及该错误的一些特性:

  • 我使用 QTextbrowser 而不是 QTextEdit,因为我希望能够插入可点击的链接,并且在 QTextbrowser 中更容易实现。
  • 我已将菜单栏分成一个名为“TextBars”的单独小部件,因为我希望菜单栏界面能够与这些菜单控制的编辑器分开存在。 (一个示例应用程序是,如果我将文本编辑器的代理放入 QGraphicsViewer 中的 QGraphicsItem,并希望从查看器外部的小部件控制此编辑器。)
  • 我已将文本编辑器配置为在双击时进入/退出“编辑模式”。在编辑模式下,编辑器接受键盘输入,菜单栏变得可见并链接到文本编辑器。

这是整个最小的示例代码。感谢任何可以提供帮助的人!

import sys
from PyQt5 import QtGui,QtWidgets



TEST_TEXT = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">\
<html><head><Meta name="qrichtext" content="1" />\
<style type="text/css">\
p,li { white-space: pre-wrap; }\
</style>\
</head>\
<body style=" font-family:\'MS Shell Dlg 2\'; font-size:7.875pt; font-weight:400; font-style:normal;">\
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">\
<span style=" font-family:\'MS Shell Dlg 2\'; font-size:6pt;">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>\
</p>\
</body>\
</html>'




class TextbrowserCustom(QtWidgets.QTextbrowser):

    def __init__(self,parent=None):
        super(TextbrowserCustom,self).__init__()
        self.is_active = False
        self.text_menu = None
        self.setHtml(TEST_TEXT)
        self.setReadOnly(True)

    def set_text_menu(self,text_menu):
        self.text_menu = text_menu

    def mouseDoubleClickEvent(self,event):
        if not self.is_active:
            self.text_menu.set_text_edit(self)
            self.setReadOnly(False)
        else:
            self.text_menu.set_text_edit(None)
            self.setReadOnly(True)
        self.is_active = not self.is_active



class TextBars(QtWidgets.QMainWindow):

    def __init__(self,parent=None):
        super(TextBars,self).__init__()
        self.text_edit = None
        self.formatbar = self.addToolBar("Format")
        self.allow_font_Box_changes = True
        self.allow_set_editor_font_size = True

    def set_text_edit(self,text_edit):
        self.text_edit = text_edit
        if self.text_edit:
            self.init_UI()
        else:
            self.clear_UI()

    def init_UI(self):
        self.fontBox = QtWidgets.QFontComboBox(self)
        self.fontBox.setMinimumContentsLength(8)
        self.fontBox.setCurrentFont(QtGui.QFont('Arial',pointSize=10,weight=-1,italic=False))
        self.fontSize = QtWidgets.QSpinBox(self)
        self.fontSize.setValue(10)
        self.fontSize.setSuffix(" pt")
        self.formatbar.addWidget(self.fontBox)
        self.formatbar.addWidget(self.fontSize)

        self.updateFontSelector()
        self.updateFontSizeSelector()

        self.fontBox.currentFontChanged.connect(self.setEditorFont)
        self.fontSize.valueChanged.connect(self.setEditorFontSize)
        self.text_edit.cursorPositionChanged.connect(self.updateFontSelector)
        self.text_edit.cursorPositionChanged.connect(self.updateFontSizeSelector)

        self.formatbar.show()

        self.setMaximumSize(10000,self.sizeHint().height())


    def updateFontSelector(self):
        cursor = self.text_edit.textCursor()
        if not (cursor.charFormat().font() == self.fontBox.currentFont()):
            self.allow_set_editor_font = False
            self.fontBox.setCurrentFont(cursor.charFormat().font())
            self.allow_set_editor_font = True

    def updateFontSizeSelector(self):
        cursor = self.text_edit.textCursor()
        if not (cursor.charFormat().fontPointSize() == self.fontSize.value()):
            self.allow_set_editor_font_size = False
            self.fontSize.setValue(cursor.charFormat().fontPointSize())
            self.allow_set_editor_font_size = True

    def setEditorFont(self,font):
        if self.allow_set_editor_font:
            self.text_edit.blockSignals(True)
            self.text_edit.setCurrentFont(font)
            self.text_edit.blockSignals(False)

    def setEditorFontSize(self,size):
        if self.allow_set_editor_font_size:
            self.text_edit.setFontPointSize(size)

    def clear_UI(self):
        self.formatbar.clear()
        self.formatbar.hide()
        self.setMaximumSize(10000,self.sizeHint().height())




if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QMainWindow()
    layout = QtWidgets.QVBoxLayout()

    text_browser_custom = TextbrowserCustom()
    text_bars = TextBars()
    text_bars.set_text_edit(None)
    text_browser_custom.set_text_menu(text_bars)

    layout.addWidget( text_bars )
    layout.addWidget( text_browser_custom )
    widget = QtWidgets.QWidget()
    widget.setLayout(layout)
    window.setCentralWidget(widget)
    window.show()
    sys.exit(app.exec_())

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)