Nikita
Nikita

Reputation: 337

MouseArea steals QQuickItem's mouse events

There's a QQuickPaintedItem with overloaded mouse events handlers:

void Plotter::mousePressEvent(QMouseEvent *event)
{
    qDebug() << "mousePressEvent";
}

void Plotter::mouseMoveEvent(QMouseEvent* event)
{
    qDebug() << "mouseMoveEvent";
}

void Plotter::hoverMoveEvent(QHoverEvent *event)
{
    qDebug() << "hoverMoveEvent";
}

I want to add a context menu to this QQuickPaintedItem in QML code, so I have to add MouseArea into this element:

Plotter {
    id: plotter
    // ...

    Menu {
        id: contextMenu
        MenuItem { text: "Добавить маркер" }
        MenuItem { text: "Удалить маркер" }
        MenuItem { text: "Удалить все маркеры" }
        MenuItem { text: "Установить шаг" }
   }        

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.RightButton
        propagateComposedEvents: true

        onClicked: {
            if (!mouseScaleButton.checked) {
                contextMenu.popup();
            }
            else
                mouse.accepted = false;

        }

    }
}

But it doesn't catch the QQuickPaintedItem's mousePressEvent if I press the right button.

Could you please explain why it happens?

Upvotes: 1

Views: 843

Answers (1)

dtech
dtech

Reputation: 49289

The MouseArea should only be enabled: !mouseScaleButton.checked.

Also, shouldn't the mouse area be below the menu? This way it blocks both the plotter and the menu.

Also, just because you have set acceptedButtons: Qt.RightButton doesn't necessarily mean it will pass through left clicks (I haven't tested it tho). You may have to enable both buttons and set mouse.accepted = false in case of a left click so it can be propagated down.

Upvotes: 1

Related Questions