QQuickPaintedItem::mouseReleaseEvent() only called after double click



  • In a class I derived from QQuickPaintedItem I try to capture a mouse release event. As found in many places I first make sure I get the event at all:

    @
    // MyItem.h
    class MyItem : public QQuickPaintedItem
    {
    public:
    MyItem(QQuickItem *parent = 0);

    protected:
    virtual void mouseReleaseEvent(QMouseEvent *event) override;
    };

    // MyItem.cpp
    MyItem::MyItem(QQuickItem *parent = 0) :
    QQuickPaintedItem(parent)
    {
    // this call is crucial to even get any clicks at all
    setAcceptedMouseButton(Qt::AllButtons);
    }
    @

    Now I would expect that I can simply implement the mouseReleaseEvent() method and get called every time I release the mouse button.

    @
    void MyItem::mouseReleaseEvent(QMouseEvent *event)
    {
    qDebug() << "Mouse x: " << event->x();
    // I even tried with and without either of the following two lines without
    // change in the result
    //QQuickPaintedItem::mouseReleaseEvent(event);
    //event->accept();
    }
    @

    However it turns out that I only get called after a double click but never after a single click.

    When I rename mouseReleaseEvent() to mousePressEvent() things work as expected. But I would prefer to handle the release case to be more consistent with the other Qt and QML components.

    Do you have any idea where this goes wrong?



  • Hi, I have the same problem. Were you able to solve it?


  • Moderators

    @Andi73 Try re-implementing mousePressEvent too.



  • I implemented also mousePressEvent.
    mousePressEvent gets called on every single mouse down but mouseReleaseEvent only after a double click. Furthermore mouseMoveEvent also gets only called after a double click and hold.


  • Moderators

    @Andi73 Works if you don't call base class mousePressEvent in mousePressEvent

    void MyItem::mousePressEvent(QMouseEvent *e) {
        qDebug() << "Pressed";
    }
    
    void MyItem::mouseReleaseEvent(QMouseEvent *e) {
        qDebug() << "Released";
        QQuickPaintedItem::mouseReleaseEvent(e);
    }
    


  • @p3c0 Thanks a lot! That works. I've looked up the code in the qt sources (QWindow.cpp) and found the following:

    void QWindow::mousePressEvent(QMouseEvent *ev)
    {
        ev->ignore();
    }
    
    void QWindow::mouseReleaseEvent(QMouseEvent *ev)
    {
        ev->ignore();
    }
    

    That explains everything... So I guess it's a bad idea calling the base class here.
    Thanks for your help!



  • Indeed @p3c0's solution did the trick. Implement mousePressEvent() empty and don't call the base class. Thanks a lot!



  • @swegmann said in QQuickPaintedItem::mouseReleaseEvent() only called after double click:

    Indeed @p3c0's solution did the trick. Implement mousePressEvent() empty and don't call the base class.

    Why does this work? @p3c0 . I m not sure I understand the relevance of calling the base class's mouse event. Why do it at all? Why does removing it solve the problem? I m trying to better understand how mouse events work. Would LOVE an explanation.


  • Lifetime Qt Champion

    Hi,

    Usually you call the base class implementation of a method to continue the normal flow of execution but do something specific to your class in addition. You don't call the base class implementation when you want to handle things completely in the subclass. This isn't specific to the mouse events.


Log in to reply