Divert mouse double click event over spin box through event filters
-
Hi, I am trying to divert the
mouseDoubleClickEvent()over aQSpinBoxto my underlyingQWidgetso 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
QSpinBoxbecause the middle part of theQSpinBoxis 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? -
@Pl45m4 Thanks, I tried it, but the
QSpinBoxcatches 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
eventFilteronqAppthen it will filter the event before anything else and then I can catch it by looking at thespinBoxgeometry, but that is overkill for such a small purpose. I haven't tried it yet because installing event filters onqAppis 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...
-
Hi, I am trying to divert the
mouseDoubleClickEvent()over aQSpinBoxto my underlyingQWidgetso 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
QSpinBoxbecause the middle part of theQSpinBoxis 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
QSpinBoxgeometry.
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 -
You could try to check against the
QSpinBoxgeometry.
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
QSpinBoxcatches 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
eventFilteronqAppthen it will filter the event before anything else and then I can catch it by looking at thespinBoxgeometry, but that is overkill for such a small purpose. I haven't tried it yet because installing event filters onqAppis 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...
-
@Pl45m4 Thanks, I tried it, but the
QSpinBoxcatches 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
eventFilteronqAppthen it will filter the event before anything else and then I can catch it by looking at thespinBoxgeometry, but that is overkill for such a small purpose. I haven't tried it yet because installing event filters onqAppis 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...
-
@CJha If you want to stick with the eventFilter method, it can be installed on each child of the QSpinBox. Eg:
for (QWidget *child: spinBox.findChildren<QWidget *>()) child->installEventFilter(this);