gpt4 book ai didi

python - 字体大小更改后 QTextEdit 中的 ZoomIn 没有影响

转载 作者:太空宇宙 更新时间:2023-11-03 11:09:46 27 4
gpt4 key购买 nike

此代码运行一个带有工具栏和 QTextEdit 区域的小窗口。

如果您突出显示“bananas”并更改字体大小,则使用工具栏按钮或 CTRL + 鼠标滚轮进行缩放只会调整“apples”的大小。有人知道为什么吗?

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_()

最佳答案

QTextEdit.zoomIn/Out 的默认实现只是更改文本编辑的基本字体的 pointSize

示例中用于更改字体大小的方法将所选单词包装在 span 标记中,并使用内联 css 将 font-size 属性设置为固定值.这意味着当文本编辑随后被缩放时,只有未更改的文本会受到影响。

使用相对字体大小可以解决这个问题。然而,它看起来只是 css properties 的一个有限子集。受支持,因此只能设置不精确的值,如 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)

关于python - 字体大小更改后 QTextEdit 中的 ZoomIn 没有影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8016530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com