是否可以缩放(或"缩放")QTextEdit区域?我相信我读到放置QTextEdit在QLayout可以允许缩放QTextEdit区域,虽然没有找到如何实现它。几个选项…
CTRL +滚动鼠标滚轮
运行下面的代码,按住CTRL (control)键并滚动鼠标滚轮,事件被捕获并且文本确实缩放(至少在Windows上),然而,随着文本变大,滚轮必须移动得越来越远,所以一个目标是能够以某种方式修改,也许是一些数学来增加增量到更大程度的加号方向。
(下面的setReadOnly()是因为它看起来texttedit必须为ReadOnly(False)才能捕获鼠标事件,然后为True才能在鼠标滚轮滚动期间缩放,因此在释放CTRL键时它又被设置回原始状态False)
工具栏按钮点击
另一个选项是工具栏按钮用于放大和缩小。
onzoominclickt()被调用。
下面代码的一些当前问题1. 上面印着:QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout
,我还没想好这个。
2. QtGui.QTextEdit(self. formlayout)代替(self)将texttedit区域放置在布局中产生TypeError: 'PySide.QtGui.QTextEdit' called with wrong argument types
3.wheelEvent()可以使用一些方法来修改event.delta()吗?
4. 工具栏按钮(仅限文本)当前将在单击时运行其def,但是它只包含一个print语句。
from PySide import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.formLayout = QtGui.QFormLayout(self)
self.textEdit = QtGui.QTextEdit(self)
self.toolBar = QtGui.QToolBar(self)
self.actionZoomIn = QtGui.QAction(self)
self.textEdit.setHtml('<font color=blue>Hello <b>world</b></font>')
self.setCentralWidget(self.textEdit)
self.addToolBar(self.toolBar)
self.toolBar.addAction(self.actionZoomIn)
self.actionZoomIn.setText('Zoom In')
self.actionZoomIn.connect(self.actionZoomIn,
QtCore.SIGNAL('triggered()'), self.onZoomInClicked)
def onZoomInClicked(self):
print "onZoomInClicked(self) needs code"
def wheelEvent(self, event):
print "wheelEvent() captured"
if (event.modifiers() & QtCore.Qt.ControlModifier):
self.textEdit.setReadOnly(True)
event.accept()
def keyReleaseEvent(self, evt):
if evt.key() == QtCore.Qt.Key_Control:
self.textEdit.setReadOnly(False)
if __name__ == '__main__':
app = QtGui.QApplication([])
frame = MainWindow()
frame.show()
app.exec_()
我一直在努力解决这个问题,所以如果有更多的可定制的QTextEdit缩放/缩放工作,如果它是可能的,那将是伟大的。
这两个错误信息可以解释如下:
-
QMainWidget
自动获得布局,QFormLayout
是冗余的。如果您想要添加一个布局,请创建一个QWidget
作为中心小部件,并使其成为新布局的父组件。其他小部件可以添加到新的布局。 -
QWidget
子类的父类必须是QWidget
子类,而QFormLayout
不是。
我已经修改了你的例子,所以它做了大部分你要求的。注意QTextEdit。放大和QTextEdit。zoomOut都有一个range
参数来控制缩放的程度。
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.actionZoomIn = QtGui.QAction('Zoom In', self)
self.actionZoomOut = QtGui.QAction('Zoom Out', self)
self.textEdit.setHtml('<font color=blue>Hello <b>world</b></font>')
self.setCentralWidget(self.textEdit)
self.addToolBar(self.toolBar)
self.toolBar.addAction(self.actionZoomIn)
self.toolBar.addAction(self.actionZoomOut)
self.actionZoomIn.triggered.connect(self.onZoomInClicked)
self.actionZoomOut.triggered.connect(self.onZoomOutClicked)
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)
def zoom(self, delta):
if delta < 0:
self.zoomOut(1)
elif delta > 0:
self.zoomIn(5)
def wheelEvent(self, event):
if (event.modifiers() & QtCore.Qt.ControlModifier):
self.zoom(event.delta())
else:
QtGui.QTextEdit.wheelEvent(self, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()