# Using EventFilters on qt objects

**URL:** <https://discourse.slicer.org/t/using-eventfilters-on-qt-objects/9744>\
**Category:** Support\
**Created:** [January 9, 2020, 12:53am UTC](https://discourse.slicer.org/t/using-eventfilters-on-qt-objects/9744 "2020-01-09T00:53:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greydon\_Gilmore](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/greydon_gilmore/32/6863_2.png) [@Greydon\_Gilmore](https://discourse.slicer.org/u/Greydon_Gilmore)\
**Post date:** [January 9, 2020, 12:53am UTC](https://discourse.slicer.org/t/using-eventfilters-on-qt-objects/9744/1 "2020-01-09T00:53:24Z")

</div>

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?

```auto
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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 9, 2020, 4:46am UTC](https://discourse.slicer.org/t/using-eventfilters-on-qt-objects/9744/2 "2020-01-09T04:46:33Z")

</div>

See a complete, working example in [SegmentEditorThresholdEffect.py](https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Segmentations/EditorEffects/Python/SegmentEditorThresholdEffect.py) (HistogramEventFilter).

---

<div class="post-metadata">

**Author:** ![Greydon\_Gilmore](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/greydon_gilmore/32/6863_2.png) [@Greydon\_Gilmore](https://discourse.slicer.org/u/Greydon_Gilmore)\
**Post date:** [January 9, 2020, 4:28pm UTC](https://discourse.slicer.org/t/using-eventfilters-on-qt-objects/9744/3 "2020-01-09T16:28:03Z")

</div>

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:

```auto
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](https://doc.qt.io/archives/qtjambi-4.5.2_01/com/trolltech/qt/core/QEvent.Type.html). 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:

```auto
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
