更改字体大小后QTextEdit中的ZoomIn无效

4 投票
1 回答
1320 浏览
提问于 2025-04-17 05:40

这段代码会打开一个小窗口,里面有一个工具栏和一个文本编辑区域。

如果你选中“香蕉”这个词并且改变它的字体大小,然后使用工具栏上的缩放按钮或者按住CTRL再滚动鼠标滚轮,只有“苹果”这个词的大小会改变。有人知道这是为什么吗?

from PySide import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.textEdit = Editor(self)
        self.toolBar  = QtGui.QToolBar(self)
        self.addToolBar(self.toolBar)
        self.setCentralWidget(self.textEdit)
        self.textEdit.setHtml('<font color=blue>apples bananas</font>')

        # Zoom
        self.actionZoomIn  = QtGui.QAction('Zoom In',  self)
        self.actionZoomOut = QtGui.QAction('Zoom Out', self)
        self.toolBar.addAction(self.actionZoomIn)
        self.toolBar.addAction(self.actionZoomOut)
        self.actionZoomIn.triggered.connect(self.onZoomInClicked)
        self.actionZoomOut.triggered.connect(self.onZoomOutClicked)

        # Font Size
        self.comboSize = QtGui.QComboBox(self.toolBar)
        self.toolBar.addWidget(self.comboSize)
        self.comboSize.addItem('0')
        self.comboSize.addItem('10')
        self.comboSize.addItem('18')
        self.comboSize.addItem('30')
        self.comboSize.addItem('48')
        self.comboSize.setCurrentIndex(1)
        self.comboSize.activated[str].connect(self.textSize)

    def textSize(self, pointSize):
        pointSize = int(pointSize)
        if pointSize > 0:
            fmt = QtGui.QTextCharFormat()
            fmt.setFontPointSize(pointSize)
            self.mergeFormatOnWordOrSelection(fmt)

    def mergeFormatOnWordOrSelection(self, format):
        cursor = self.textEdit.textCursor()
        if not cursor.hasSelection():
            cursor.select(QtGui.QTextCursor.WordUnderCursor)

        cursor.mergeCharFormat(format)
        self.textEdit.mergeCurrentCharFormat(format)

    def onZoomInClicked(self):
        self.textEdit.zoom(+1)

    def onZoomOutClicked(self):
        self.textEdit.zoom(-1)

class Editor(QtGui.QTextEdit):
    def __init__(self, parent=None):
        super(Editor, self).__init__(parent)
        self.zoomValue = 0

    def zoom(self, delta):
        zoomIncrement = 3

        if delta < 0:
            zoomIncrement = 0 - zoomIncrement

        self.zoomIn(zoomIncrement)
        self.zoomValue = self.zoomValue + zoomIncrement

        print "self.zoomValue", self.zoomValue

    def wheelEvent(self, event):
        if (event.modifiers() & QtCore.Qt.ControlModifier):
            self.zoom(event.delta())

if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = MainWindow()
    window.resize(400, 180)
    window.show()
    app.exec_()

1 个回答

4

默认情况下,QTextEdit.zoomIn/Out的实现方式只是简单地改变文本编辑器中基础字体的pointSize

在这个例子中,改变字体大小的方法是把选中的单词包裹在一个span标签里,并使用内联CSS来设置font-size属性为一个固定值。这意味着,当文本编辑器后续进行缩放时,只有那些没有被改变的文本会受到影响。

要解决这个问题,可以使用相对字体大小。不过,看起来只支持有限的CSS属性,所以只能设置一些不太精确的值,比如smalllarge等。

在这个例子中,可以通过以下更改来实现:

    # Font Size
    self.comboSize = QtGui.QComboBox(self.toolBar)
    self.toolBar.addWidget(self.comboSize)
    self.comboSize.addItem('small')
    self.comboSize.addItem('medium')
    self.comboSize.addItem('large')
    self.comboSize.addItem('x-large')
    self.comboSize.addItem('xx-large')
    self.comboSize.setCurrentIndex(1)
    self.comboSize.activated[int].connect(self.textSize)

def textSize(self, size):
    fmt = QtGui.QTextCharFormat()
    fmt.setProperty(QtGui.QTextFormat.FontSizeAdjustment, size - 1)
    self.mergeFormatOnWordOrSelection(fmt)

撰写回答