Divert mouse double click event over spin box through event filters
-
Hi, I am trying to divert the
mouseDoubleClickEvent()
over aQSpinBox
to my underlyingQWidget
so that I can process it. For this I have created aneventFilter()
in my underlying widget:bool CustomGui::eventFilter(QObject* watched, QEvent* event) { if(event->type() == QEvent::MouseButtonDblClick) { QMouseEvent* evt = static_cast<QMouseEvent*>(event); mouseDoubleClickEvent(evt); return true; } else return false; }
But this only gets triggered when I click exactly on the border of the
QSpinBox
because the middle part of theQSpinBox
is actually aQLineEdit
. I cannot install the event filter on the spin box's line-edit because access to it is only provided through a protected function which means I will have to subclass theQSpinBox
. Is there any way around that I can filter out the double clicks on aQSpinBox
? -
You could try to check against the
QSpinBox
geometry.
If you doubleClick is inside the QSpinBox rect on parent widget, you've hit it.
(Not tested, just a thought)Edit:
Maybe you can utilizeqApp->widgetAt(....)
somehow -
@Pl45m4 Thanks, I tried it, but the
QSpinBox
catches themouseDoubleClickEvent()
and it is not received by my underlying widget:void CustomGui::mouseDoubleClickEvent(QMouseEvent* event) { qDebug() << "Mouse Double Click!"; if(ui.spinBox->rect().contains(event->pos())) { qDebug() << "on Spin Box"; } }
Anywhere else it produces Mouse Double Click!, but not on
spinBox
.The only way I could think of right now is to install an
eventFilter
onqApp
then it will filter the event before anything else and then I can catch it by looking at thespinBox
geometry, but that is overkill for such a small purpose. I haven't tried it yet because installing event filters onqApp
is not recommended:It is also possible to filter all events for the entire application, by installing an event filter on the QApplication or QCoreApplication object. Such global event filters are called before the object-specific filters. This is very powerful, but it also slows down event delivery of every single event in the entire application...