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?
-
@Andi73 Works if you don't call base class
mousePressEvent
inmousePressEvent
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! -
@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.
-
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.