当鼠标不在QTextEdit上时转动鼠标滚轮时,滚动条不会移动,但我仍然想通过鼠标滚轮移动滚动条,那么我该如何实现这个功能呢?我知道有些软件像 Microsoft Word 有这个功能。
我实现了这个功能,如下所示,但是当您通过鼠标滚轮将滚动条移动到顶部或底部时,会出现错误:调用 Python 对象时超出最大递归深度。任何人都可以帮忙吗?我的代码在这里http://codepad.org/1rq06qTk:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class BoxLayout(QWidget):
def __init__(self, parent=None):
super(BoxLayout, self).__init__(parent)
self.resize(100, 300)
ok = QPushButton("OK")
cancel = QPushButton("Cancel")
self.textEdit = QTextEdit("This function returns true if the contents "
"of the MIME data object, specified by source, "
"can be decoded and inserted into the document. "
"It is called for example when during a drag "
"operation the mouse enters this widget and it "
"is necessary to determine whether it is possible "
"to accept the drag and drop operation.")
vbox = QVBoxLayout()
vbox.addWidget(self.textEdit)
vbox.addWidget(ok)
vbox.addWidget(cancel)
self.setLayout(vbox)
# self.textEdit.installEventFilter(self)
# def eventFilter(self, obj, event):
# if obj == self.textEdit:
# if event.type() == QEvent.Wheel:
# self.textEdit.wheelEvent(event)
# return True
# else:
# return False
# else:
# return QMainWindow.eventFilter(self, obj, event)
def wheelEvent(self, event):
self.textEdit.wheelEvent(event)
app = QApplication(sys.argv)
qb = BoxLayout()
qb.show()
sys.exit(app.exec_())