Felix.leg
Felix.leg

Reputation: 769

Disable context menu from qml in WebEngineView

I'm programming a WebEngineView and I want to disable its context menu. For what I already found I have to call QWidget's setContexMenuPolicy. Unfortunately, all help in the Net I can find shows how to do it from C++ code, while I need to do it all from my .qml file (I have no access to the c++ code).

I tried this.setContextMenuPolicy(...) from Component.onCompleted signal inside the WebEngineView, but to no success.

Upvotes: 0

Views: 1167

Answers (2)

iam_peter
iam_peter

Reputation: 3924

You can't just access QWidget functions in QML if they aren't forwarded via Q_PROPERTY. You should read The Property System.

My solution is a bit of a hack. It's using a MouseArea which consumes right mouse clicks, basically blocks all right clicks on the WebEngineView:

UPDATE!

import QtQuick 2.0
import QtQuick.Window 2.0
import QtWebEngine 1.0

Window {
    width: 1024
    height: 750
    visible: true
    WebEngineView {
        anchors.fill: parent
        url: "https://www.qt.io"
    }

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.MiddleButton | Qt.RightButton
        onPressed: {
            if (mouse.button === Qt.RightButton)
                mouse.accepted = true
        }
    }
}

Upvotes: 0

Felix.leg
Felix.leg

Reputation: 769

I found other way, which works for my case:

WebEngineView {
    anchors.fill: parent
    url: "https://www.example.com"
    onContextMenuRequested: {
        request.accepted = true
    }
}

Upvotes: 3

Related Questions