Issue
I would like to be able to scroll with the mouse inside QScrollArea regardless if the mouse is positioned above disabled QToolButton or not. The documentation mentions disabled widgets don't propagate mouse events to parents and i figured it out i need to install event filter to overcome this issue i am experiencing. This is how far i managed to do it. I think the event filter is installed as wheel event is detected but i am not sure what should be the next steps:
from PySide import QtGui
from PySide.QtCore import *
import sys
app = QtGui.QApplication(sys.argv)
wid = QtGui.QWidget()
wid.resize(200, 200)
scroll = QtGui.QScrollArea()
scroll.setWidget(wid)
layout = QtGui.QVBoxLayout()
wid.setLayout(layout)
class installEvent(QObject):
def eventFilter(self, i_obj, i_event):
if i_event.type() == QEvent.Wheel:
print "QEvent: " + str(i_event.type())
buttonEnabled = QtGui.QToolButton()
layout.addWidget(buttonEnabled)
buttonDisabled = QtGui.QToolButton()
buttonDisabled.setEnabled(False)
layout.addWidget(buttonDisabled)
buttonDisabled.installEventFilter(installEvent(buttonDisabled))
scroll.show()
sys.exit(app.exec_())
Solution
Once you have caught the correct event, you need to post the event to the parent widget. I've done this before like this:
def eventFilter(self, obj, event):
if obj and not obj.isEnabled() and event.type() == QEvent.Wheel:
newEvent = QWheelEvent(obj.mapToParent(event.pos()), event.globalPos(),
event.delta(), event.buttons(),
event.modifiers(), event.orientation())
QApplication.instance().postEvent(obj.parent(), newEvent)
return True
return QObject.eventFilter(self, obj, event)
This event filter only does something if the button is disabled and the event type is QEvent.Wheel
. Otherwise it implements the default behaviour of eventFilter
.
The login inside the if statement first maps the coordinates to the parent widget, before constructing a new QWEheelEvent
with the identical parameters as the original event (except for the coordinate mapping). The final part is to grab an instance of your QApplication
and use the postEvent
method to send the event to the Qt event loop, where Qt will distribute the event to the appropriate widget (the parent of the disabled button).
Answered By - three_pineapples
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.