Michael Shcherbakov
Michael Shcherbakov

Reputation: 154

How to correctly override mousePressEvent in qquickwindow?

I have an object inherited from qquickwindow with an overridden mousePressEvent method.

.h

class FWindow : public QQuickWindow
{
    Q_OBJECT

public:
    FWindow(QQuickWindow* parent = Q_NULLPTR);

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

.cpp

void FWindow::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        ...
    }
    QQuickWindow::mousePressEvent(event);
}

The problem is that when I add a Rectangle with MouseArea to a qml file, it does not react in any way. The signal goes to FWindow, not MouseArea. How to fix it?

.qml

FWindow
{
    visible: true;

    Rectangle
    {
        width: 50;
        height: 50;
        color: "green";

        anchors.verticalCenter: parent.verticalCenter;
        anchors.horizontalCenter: parent.horizontalCenter;

        MouseArea
        {
            anchors.fill: parent;

            onClicked:
            {
                console.log("clicked");
            }
        }
    }
}

Upvotes: 1

Views: 1515

Answers (1)

Silvano Cerza
Silvano Cerza

Reputation: 970

The documentation says that the received QMouseEvent* event in QQuickItem::mousePressEvent is accepted by default, if you don't want to accept it you must call event->ignore().

Upvotes: 2

Related Questions