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. Catch 'DoubleMouseClick' inside 'mouseMoveEvent' QtabWidget
Forum Updated to NodeBB v4.3 + New Features

Catch 'DoubleMouseClick' inside 'mouseMoveEvent' QtabWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtabwidgetmouseeventmousemovebuttonqt6.5
13 Posts 6 Posters 1.7k 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.
  • N Narutoblaze
    4 Jul 2023, 15:32

    I have custom QtabWidget where i am handling mouse event myself i want to handle double mouse click event & left mouse button click event by myself but i am unable to find a way to do this.

    I have tried this way but it is not working and when i debugged it shows no button is clicked and also i could not find anything like "Qt::DoubleClick"

    void CustomTab::mouseMoveEvent(QMouseEvent *e)
    {
    qDebug() << e->buttons();
    
    /*
    if(e->buttons() == Qt::LeftButton)
    {
      // Do Something
    } 
    
    if(e->buttons() == Qt::DoubleClick)
    {
      // Do Something
    } 
    */
    }
    

    Debug Output :

    QFlags<Qt::MouseButton> (NoButton)
    
    G Offline
    G Offline
    giusdbg
    wrote on 4 Jul 2023, 15:52 last edited by
    #3

    @Narutoblaze

    QTabWidget Class
    ..........................
    Signals
    void currentChanged(int index)
    void tabBarClicked(int index)
    void tabBarDoubleClicked(int index)
    void tabCloseRequested(int index)

    only for example

    connect(tabWidget->tabBar(), &QTabBar::tabBarDoubleClicked,
    this, MyWidget::editTabBarLabel);

    1 Reply Last reply
    2
    • N Narutoblaze
      4 Jul 2023, 15:32

      I have custom QtabWidget where i am handling mouse event myself i want to handle double mouse click event & left mouse button click event by myself but i am unable to find a way to do this.

      I have tried this way but it is not working and when i debugged it shows no button is clicked and also i could not find anything like "Qt::DoubleClick"

      void CustomTab::mouseMoveEvent(QMouseEvent *e)
      {
      qDebug() << e->buttons();
      
      /*
      if(e->buttons() == Qt::LeftButton)
      {
        // Do Something
      } 
      
      if(e->buttons() == Qt::DoubleClick)
      {
        // Do Something
      } 
      */
      }
      

      Debug Output :

      QFlags<Qt::MouseButton> (NoButton)
      
      C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 4 Jul 2023, 16:00 last edited by Chris Kawa 7 Apr 2023, 16:01
      #4

      @Narutoblaze Sorry, but what you posted makes no sense. Moving a mouse and pressing its buttons are two different events. You can't "catch a click inside move". What does that even mean? e->buttons() returns the state of the buttons when given event occurs. When you check it within mouse move handler it reports the state of the buttons at the time the mouse was moved. It doesn't report any clicks.

      A click is a press and release over the same widget. A double click is press, release, press, release over the same widget with a small time window. When that happens a widget gets these event handlers in order:

      mousePressEvent
      mouseReleaseEvent
      mousePressEvent
      mouseDoubleClickEvent
      mouseReleaseEvent

      so if you want to handle double click manually then override mouseDoubleClickEvent, not mouseMoveEvent, which happens when the mouse is moved over a widget. As @giusdbg mentioned, in case of QTabWidget, you can also connect to the tabBarDoubleClicked signal it emits.

      Also, important thing to remember is, when you do override these events, you should either call the base implementation or explicitly call e->accept() or e->ignore() to actually handle the event. Not doing this leaves the widget in inconsistent state if it does anything special in the base implementation, which is likely in case of a tab widget.

      1 Reply Last reply
      4
      • N Offline
        N Offline
        Narutoblaze
        wrote on 4 Jul 2023, 17:33 last edited by
        #5

        @Chris-Kawa @Christian-Ehrlicher @giusdbg I apologize for not providing full context of what i have done and want to do further.

        This is what i am doing :

        void CustomTab::mouseMoveEvent(QMouseEvent *e)
        {
        
            // IF Not Mouse Double CLicked 
            // if(e->buttons() != Qt::MouseDoubleClicked)
            //      return;
        
            if (!tabBar()->tabRect(tabBar()->tabAt(e->pos())).contains(e->pos()))
                return;
        
            QTabBar *tabbar = tabBar();
            QRect tabrect = tabbar->tabRect(tabbar->tabAt(e->pos()));
            QPixmap pix = QPixmap(tabrect.size());
            tabbar->render(&pix, QPoint(), QRegion(tabrect));
        
            QCursor cursor = QCursor(Qt::OpenHandCursor);
            QDrag drag = QDrag(tabbar);
        
            drag.setHotSpot(e->pos() - tabbar->mapFromGlobal(mapToGlobal(e->pos())));
            drag.setDragCursor(cursor.pixmap(), Qt::MoveAction);
            drag.setMimeData(new QMimeData());
            drag.setPixmap(pix);
            drag.exec(Qt::MoveAction);
        }
        

        I am making use of mouseMoveEvent and i want to drag the tabbar only if double clicked when moving and as @Chris-Kawa said it only tells the state i was able to catch Qt::RightButton but could not catch Qt::LeftButton even i was clicking while moving.

        J 1 Reply Last reply 4 Jul 2023, 17:46
        0
        • N Narutoblaze
          4 Jul 2023, 17:33

          @Chris-Kawa @Christian-Ehrlicher @giusdbg I apologize for not providing full context of what i have done and want to do further.

          This is what i am doing :

          void CustomTab::mouseMoveEvent(QMouseEvent *e)
          {
          
              // IF Not Mouse Double CLicked 
              // if(e->buttons() != Qt::MouseDoubleClicked)
              //      return;
          
              if (!tabBar()->tabRect(tabBar()->tabAt(e->pos())).contains(e->pos()))
                  return;
          
              QTabBar *tabbar = tabBar();
              QRect tabrect = tabbar->tabRect(tabbar->tabAt(e->pos()));
              QPixmap pix = QPixmap(tabrect.size());
              tabbar->render(&pix, QPoint(), QRegion(tabrect));
          
              QCursor cursor = QCursor(Qt::OpenHandCursor);
              QDrag drag = QDrag(tabbar);
          
              drag.setHotSpot(e->pos() - tabbar->mapFromGlobal(mapToGlobal(e->pos())));
              drag.setDragCursor(cursor.pixmap(), Qt::MoveAction);
              drag.setMimeData(new QMimeData());
              drag.setPixmap(pix);
              drag.exec(Qt::MoveAction);
          }
          

          I am making use of mouseMoveEvent and i want to drag the tabbar only if double clicked when moving and as @Chris-Kawa said it only tells the state i was able to catch Qt::RightButton but could not catch Qt::LeftButton even i was clicking while moving.

          J Offline
          J Offline
          JonB
          wrote on 4 Jul 2023, 17:46 last edited by
          #6

          @Narutoblaze
          Like @Chris-Kawa said I don't understand how you would double-click while moving. And you are doing a drag operation, so won't any mouse release end it anyway?

          Are you sure you don't mean something like you want a double-click to start moving the tab bar, not double-click when moving?

          N 1 Reply Last reply 4 Jul 2023, 17:47
          0
          • J JonB
            4 Jul 2023, 17:46

            @Narutoblaze
            Like @Chris-Kawa said I don't understand how you would double-click while moving. And you are doing a drag operation, so won't any mouse release end it anyway?

            Are you sure you don't mean something like you want a double-click to start moving the tab bar, not double-click when moving?

            N Offline
            N Offline
            Narutoblaze
            wrote on 4 Jul 2023, 17:47 last edited by
            #7

            @JonB double-click to start moving the tab bar, not double-click when moving?

            YES !

            J C M 3 Replies Last reply 4 Jul 2023, 17:55
            0
            • N Narutoblaze
              4 Jul 2023, 17:47

              @JonB double-click to start moving the tab bar, not double-click when moving?

              YES !

              J Offline
              J Offline
              JonB
              wrote on 4 Jul 2023, 17:55 last edited by
              #8

              @Narutoblaze
              Well that is a totally different question!

              By the time you are in mouseMoveEvent(), and about to start your drag, it's too late to tell whether you invoked it via a double-click. And you cannot tell from mouse button state, that will tell you it's left or right button, and modifier keys, but not what clicking happened.

              From @Chris-Kawa's order of events, either start your drag in mouseDoubleClickEvent() or handle that to note the time and in mouseMoveEvent() compare time now against that (e.g. use a class QElapsedTimer) to see whether the double-click happened "recently" enough that you want to allow drag.

              1 Reply Last reply
              1
              • N Narutoblaze
                4 Jul 2023, 17:47

                @JonB double-click to start moving the tab bar, not double-click when moving?

                YES !

                C Offline
                C Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on 4 Jul 2023, 18:05 last edited by
                #9

                @Narutoblaze First of all it's very weird what you're doing. A drag operation means you're dragging something i.e. grab it and move it while holding it, so starting a drag on double click (remember, double click is press, release, press, release) is very uncommon and not user friendly.

                But anyway, assuming there's some deeper reason for doing such a thing, you're doing it wrong. If you really want to start a drag after the double click then no button will be pressed at the time of move. Yet again - double click is press, release, press, release. So to do drag on double click you would override mouseDoubleClickEvent. If you want to start the drag on double click that's where you do your drag code. If you want to do drag on move after double click then do as @JonB said - note the time (also mouse pos and some bool to indicate that a double click happened) and in the move event check if the time difference is under a limit and you moved far enough from the click place and then start your drag code.

                Important things to note:

                • Since you want to get a move after a double click no mouse button will be pressed at that time. Widgets by default don't get mouse move events when no button is pressed. To get those events you need to enable mouse tracking on your widget.
                • The minimum distance to start a drag is platform specific and you should use QStyleHints::startDragDistance() to get it.
                • Some platforms also use minimum velocity for drag operations. You should get it with QStyleHints::startDragVelocity()
                • Theres' also a QStyleHints::startDragTime() that denotes the minimum holdtime for a drag operation to start, but it doesn't really fit your case. Again- you're doing something very unusual and weird.

                All in all I think you should rethink that double-click design. A drag is typically initiated by a press and move, not double click (press release press release) and move.

                N 1 Reply Last reply 4 Jul 2023, 18:37
                1
                • N Narutoblaze
                  4 Jul 2023, 17:47

                  @JonB double-click to start moving the tab bar, not double-click when moving?

                  YES !

                  M Offline
                  M Offline
                  mpergand
                  wrote on 4 Jul 2023, 18:29 last edited by mpergand 7 Apr 2023, 18:30
                  #10

                  @Narutoblaze
                  You can :
                  -set a bool var to true in mouseDoubleClickEvent
                  -check it out in mouseMoveEvent
                  -then set the bool to false in mouseReleaseEvent

                  1 Reply Last reply
                  0
                  • C Chris Kawa
                    4 Jul 2023, 18:05

                    @Narutoblaze First of all it's very weird what you're doing. A drag operation means you're dragging something i.e. grab it and move it while holding it, so starting a drag on double click (remember, double click is press, release, press, release) is very uncommon and not user friendly.

                    But anyway, assuming there's some deeper reason for doing such a thing, you're doing it wrong. If you really want to start a drag after the double click then no button will be pressed at the time of move. Yet again - double click is press, release, press, release. So to do drag on double click you would override mouseDoubleClickEvent. If you want to start the drag on double click that's where you do your drag code. If you want to do drag on move after double click then do as @JonB said - note the time (also mouse pos and some bool to indicate that a double click happened) and in the move event check if the time difference is under a limit and you moved far enough from the click place and then start your drag code.

                    Important things to note:

                    • Since you want to get a move after a double click no mouse button will be pressed at that time. Widgets by default don't get mouse move events when no button is pressed. To get those events you need to enable mouse tracking on your widget.
                    • The minimum distance to start a drag is platform specific and you should use QStyleHints::startDragDistance() to get it.
                    • Some platforms also use minimum velocity for drag operations. You should get it with QStyleHints::startDragVelocity()
                    • Theres' also a QStyleHints::startDragTime() that denotes the minimum holdtime for a drag operation to start, but it doesn't really fit your case. Again- you're doing something very unusual and weird.

                    All in all I think you should rethink that double-click design. A drag is typically initiated by a press and move, not double click (press release press release) and move.

                    N Offline
                    N Offline
                    Narutoblaze
                    wrote on 4 Jul 2023, 18:37 last edited by Narutoblaze 7 Apr 2023, 18:38
                    #11

                    @Chris-Kawa @mpergand Thanks for clearing out for me. I have tried overriding mouseDoubleClickEvent but it does not get called when clicked over tabbar area

                    void CustomTab::mouseDoubleClickEvent(QMouseEvent *e)
                    {
                          qDebug() << "Double Clicked " << e->pos();
                    }
                    

                    working when clicked on empty area not working when mouse is top of tabbar

                    C G 2 Replies Last reply 4 Jul 2023, 19:08
                    0
                    • N Narutoblaze
                      4 Jul 2023, 18:37

                      @Chris-Kawa @mpergand Thanks for clearing out for me. I have tried overriding mouseDoubleClickEvent but it does not get called when clicked over tabbar area

                      void CustomTab::mouseDoubleClickEvent(QMouseEvent *e)
                      {
                            qDebug() << "Double Clicked " << e->pos();
                      }
                      

                      working when clicked on empty area not working when mouse is top of tabbar

                      C Offline
                      C Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on 4 Jul 2023, 19:08 last edited by
                      #12

                      @Narutoblaze That's expected. When you click on an empty area you're clicking on the tab widget. When you click on the bar then you're clicking, well, on the bar, not the tab widget. If you want the click on the bar subclass QTabBar and set it on the tab widget using setTabBar().

                      1 Reply Last reply
                      3
                      • N Narutoblaze
                        4 Jul 2023, 18:37

                        @Chris-Kawa @mpergand Thanks for clearing out for me. I have tried overriding mouseDoubleClickEvent but it does not get called when clicked over tabbar area

                        void CustomTab::mouseDoubleClickEvent(QMouseEvent *e)
                        {
                              qDebug() << "Double Clicked " << e->pos();
                        }
                        

                        working when clicked on empty area not working when mouse is top of tabbar

                        G Offline
                        G Offline
                        giusdbg
                        wrote on 5 Jul 2023, 11:13 last edited by
                        #13

                        @Narutoblaze Since I recently burned my feathers, I strongly advise you not to try to go against how the automatisms and things in general have been built, especially if it's just because you're used to doing it in a certain way.

                        Use the signals available, use installEventFilter, avoid complications just to look cool, simplify the goals.

                        You'll do the cool stuff in the future, after you get used to it.

                        1 Reply Last reply
                        1

                        12/13

                        4 Jul 2023, 19:08

                        • Login

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