【发布时间】:2013-07-19 05:56:05
【问题描述】:
当鼠标不在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_())
【问题讨论】: