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

Event when the QMouseMoveEvent is finished (released)?



  • Is there an event when I first click, then move mouse, then release mouse, that is emitted at the last step (QMouseReleaseEvent does only occure when no movement happend)

    Or if not, how can I detect this?


  • Lifetime Qt Champion

    Hi
    QMouseReleaseEvent comes when you release the
    mouse button so why cant you use that ?

    If you think its not the last step, its due to you moving the mouse after you release.

    You should just code your app so its not important that QMouseReleaseEvent is not the last you see.
    Just check if button is pressed before doing anything in QMouseMoveEvent and then it wont matter if
    MouseRelease is not the last event.



  • @gde23

    Hi,

    probably there is a better solution, but I would set a bool to true when mouse click happens, temporarily save the point as well, then compare the QPoints of mouse press and mouse release in mouseReleaseEvent and do what you want to do, if they don't match...

    But yeah, normally you dont have to do things like this. You dont need mousePress, -release and moveEvents to detect if user drags an object with the mouse. (like @mrjj said)


  • Moderators

    @gde23
    the problem is probably, that your Mose leaves the area of the Widget where you override the event.
    IIRC if the mouse is not inside the widget bounds, the released slot is not triggered.

    But the leaveEvent should



  • @J-Hilk

    You can experience this behavior in some programs, where you dont expect such unhandled events.
    (If you drag something with mouse and leave the window or drag-area, the item is still stuck to your mouse, virtually. So when you enter the area / window again somewhere else, the item moved around the area and flies in at the position where the mouse has entered)
    In some cases it is useful, in some cases it can be annoying :)



  • QMouseReleaseEvent() is not called in my case.
    Some more details: The widget is a TitelBarWidget which is set on a QDockWidget.
    What I want to detect is when it has been moved (click titelbarwidget, move around, release mousue)
    So maybee this is not a MouseMoveEvent but a MoveEvent?



  • @gde23

    mouseReleaseEvent should be triggered, if you release the mouse button. It depends where you release the mouse.

    @gde23 said in Event when the QMouseMoveEvent is finished (released)?:

    So maybee this is not a MouseMoveEvent but a MoveEvent?

    MoveEvent is triggered, when you move the whole item or widget. That means, that you need to actually move the titlebar widget around on its parent window...

    Do you want to drag / move something inside your QDockWidget or is it all about detecting the movement of the QDockWidget window itself (then you can indeed use the moveEvent)?
    https://doc.qt.io/archives/qt-4.8/qwidget.html#moveEvent



  • Ok, I did not know about MoveEvent.

    What I want to do: By grabbing the titlebarwidget the whole widget is moved. This works.
    However at the end of the move I want to update it (not all the time during move)
    So I am searching for how to detect the end of the movement (when the mouse is released after moving)



  • @gde23

    You can combine the mousePressEvent and mouseReleaseEvent from your titlebar widget (The mouse stays within titlebar boundings during the movement, correct? Because it's standard dock window dragging behavior, with the only difference that you replaced the titlebar widget with a custom one?!) with the moveEvent from your QDockWidget.

    void TitleBarWidget::mousePressEvent(QMouseEvent *event)
    {
      m_aboutToMove = true;
    
    }
    
    void TitleBarWidget::mouseReleaseEvent(QMouseEvent *event)
    {
    
    if(m_hasMoved)
    {
     // UPDATE, send signal to update content
    // reset m_hasMoved afterwards
    }
      m_aboutToMove = false;
    
    }
    
    void YourDockWidget::moveEvent(QMoveEvent *event)
    {
    
    if(m_aboutToMove)
    {
      m_hasMoved = true; // you could send a signal to titlebar widget and write a public function to notify the titlebar
    }
    }
    

    Of course you can not access all the boolean from both classes directly... You'll need to write setters / getters or send signals.
    It is just an example or idea how it can be done :)



  • The mouse stays inside the titlebar (since it is moves along with it) and yes, I only replaced the titelbarwidget with my own version by setTitelBarWidget().

    However I've tried using the MouseReleaseEvent, but it only is called when I do not move the mouse (move the widget) in between.



  • @gde23 said in Event when the QMouseMoveEvent is finished (released)?:

    However I've tried using the MouseReleaseEvent, but it only is called when I do not move the mouse (move the widget) in between.

    Which mouseReleaseEvent do you use? From titlebar widget or from your dockwidget



  • From the titlebar


  • Moderators

    @gde23
    we won't get much further, until you post a minimal reproducible example.



  • @J-Hilk

    Here some code:

    First The dockwidget gets a titlebar widget:

    QDockWidget* dock = new QDockWidget(this); 
    titleBarWidget = new MyTitleBarWidget(dock);
    dock->setTitleBarWidget(titleBarWidget);
    

    The tiltebarwidget is just a QWidget with one single method added

    void MyTitleBarWidget::mouseReleaseEvent(QMouseEvent *event)
    {
        std::cout << "mouse Released" << std::endl;
        QWidget::mouseReleaseEvent(event);
    }
    

    Now when you click the titlebar and release it without moving the output is printed.
    However when you click it, then move around and then release it is not printed to std out.


  • Moderators

    @gde23
    this,
    https://github.com/DeiVadder/QDockWidget-Test-111758
    is a minimal reproducible example, and I can indeed reproduce it,

    as soon as the Docking mechanism is triggered(detach), the released signal is no longer emitted.

    I would consider this a bug, but I haven't checked the source code to see, if its intended or not.



  • @J-Hilk Thanks a lot for the help. I will try to think of some other solution then.



  • I finally found a method that works, that I want so share:

    void myWidget::moveEvent(QMoveEvent *event)
    {
        Qt::MouseButtons mouse = qApp->mouseButtons();
        if(mouse != Qt::LeftButton)
        {
            std::cout << "move end" << std::endl;
        }
    }
    

    If you check the mouse status in the moveEvent(), on the last move event the mouse will not be pressed anymore, which is kind of like a mouse release event. At least it works for me at the moment.