Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Divert mouse double click event over spin box through event filters



  • Hi, I am trying to divert the mouseDoubleClickEvent() over a QSpinBox to my underlying QWidget so that I can process it. For this I have created an eventFilter() 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 the QSpinBox is actually a QLineEdit. 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 the QSpinBox. Is there any way around that I can filter out the double clicks on a QSpinBox ?



  • @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);
    


  • @CJha

    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 utilize qApp->widgetAt(....) somehow

    https://doc.qt.io/qt-5/qapplication.html#widgetAt



  • @Pl45m4 Thanks, I tried it, but the QSpinBox catches the mouseDoubleClickEvent() 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 on qApp then it will filter the event before anything else and then I can catch it by looking at the spinBox geometry, but that is overkill for such a small purpose. I haven't tried it yet because installing event filters on qApp 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...



  • @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);
    


  • @jeremy_k Thanks, that is a genius solution, it worked very well!


Log in to reply