Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Divert mouse double click event over spin box through event filters
Qt 6.11 is out! See what's new in the release blog

Divert mouse double click event over spin box through event filters

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by
    #1

    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 ?

    Pl45m4P 1 Reply Last reply
    0
    • CJhaC CJha

      @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...

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by jeremy_k
      #4

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

      Asking a question about code? http://eel.is/iso-c++/testcase/

      CJhaC 1 Reply Last reply
      2
      • CJhaC CJha

        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 ?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #2

        @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


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        CJhaC 1 Reply Last reply
        1
        • Pl45m4P Pl45m4

          @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

          CJhaC Offline
          CJhaC Offline
          CJha
          wrote on last edited by CJha
          #3

          @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...

          jeremy_kJ 1 Reply Last reply
          0
          • CJhaC CJha

            @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...

            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by jeremy_k
            #4

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

            Asking a question about code? http://eel.is/iso-c++/testcase/

            CJhaC 1 Reply Last reply
            2
            • jeremy_kJ jeremy_k

              @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);
              
              CJhaC Offline
              CJhaC Offline
              CJha
              wrote on last edited by
              #5

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved