Using EventFilters on qt objects

Hi all,

I am attempting to ignore mouse wheel events on a qt.QDoubleSpinBox. This would be within a ScriptedLoadableModuleWidget.

How would I go about doing this?

def eventFilter(self, obj, event):
	if event.type() == qt.QEvent.Wheel and isinstance(obj, qt.QDoubleSpinBox):
		event.ignore()
		return True
	else:
		return False

Thank you,
Greydon

See a complete, working example in SegmentEditorThresholdEffect.py (HistogramEventFilter).

For anyone looking for the answer to this question but don’t want to scroll through over 1000 lines of code:

You need to make a separate qt.Object class:

class customEventFilter(qt.QObject):
	def eventFilter(self, obj, event):
		'''
		Event filter for rerouting wheelEvents away from double spin boxes.
		'''
		if event.type() == qt.QEvent.Wheel and isinstance(obj, qt.QDoubleSpinBox):
			#this handles all the wheel events for the double spin boxes
			event.ignore()
			return True
		
		return False

If you’d like to ignore another type of event replace qt.QEvent.Wheel with the specific qt.Event. If you’d like to apply this filter to a different object then replace qt.QDoubleSpinBox with the specific object.

You will then need to install this filter on the specific objects you’d like to inherit the filter:

self.customEventFilter = customEventFilter()

path = os.path.join(os.path.dirname(self.script_path), 'Resources', 'UI', 'eventFilterExample.ui')
self.ui = slicer.util.loadUI(path)

self.CrosshairCoords_X = self.ui.findChild(qt.QDoubleSpinBox, "CrosshairCoords_X")
self.CrosshairCoords_X.installEventFilter(self.customEventFilter)

Cheers,
Greydon

1 Like