Search code examples
pythonpython-3.xpyqtpyqt5qwebengineview

How to disable contextMenu from QWebEngineView?


I want to disable the right click menu which appears by default when you create a QWebEngineView.

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl

app = QApplication(sys.argv)

webBrowser = QWebEngineView()

#Some line here to delete the contextMenu

webBrowser.load(QUrl("https://stackoverflow.com/"))
webBrowser.show()

sys.exit(app.exec_())

In the doc we can find a class QWebEngineContextMenuData which "provides context data for populating or extending a context menu with actions..." but nothing to delete in here?


Solution

  • To disable the default widgets menu then the contextMenuPolicy must be set to Qt::NoContextMenu:

    webBrowser.setContextMenuPolicy(Qt.ContextMenuPolicy.NoContextMenu)