Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to redirect Wheel events of QWidget to QTextEdit ?
redstoneleo last edited by
when you turn the mouse wheel while the mouse cursor is not on the QTextEdit ,the scroll bars will not move in such case ,but I still want to move the scroll bars by mouse wheel ,so how can I implement this function ?
I know some software like Microsoft Word have this feature .
I implement this feature like the following ,but when you move the scroll bars to the top or bottom by mouse wheel ,an error would occur : maximum recursion depth exceeded while calling a Python object.
anyone can help ?
from PyQt4.QtGui import *
from PyQt4.QtCore import *
def init(self, parent=None):
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)
def eventFilter(self, obj, event):
if obj == self.textEdit:
if event.type() == QEvent.Wheel:
return QMainWindow.eventFilter(self, obj, event)
def wheelEvent(self, event): self.textEdit.wheelEvent(event)
app = QApplication(sys.argv)
qb = BoxLayout()
Jeroentjehome last edited by
Hmm, You might need to redirect the mouseEvent for the "parent" qidget or even for the MainWindow where all widgets are placed. Or maybe check out eventFilters. The widget that has "focus" will get the event, so you need to redirect it.