简体   繁体   中英

How to customize python PyQt5 QWebEngineView context menu?

I am a beginner and need help to be an expert. Its been a month, since I am working with PyQt5 QWebEngineView library

This is my code

# importing required libraries 
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
import os 
import sys 

class MainWindow(QMainWindow): 

    # constructor 
    def __init__(self, *args, **kwargs): 
        super(MainWindow, self).__init__(*args, **kwargs) 

        self.browser = QWebEngineView() 
        self.browser.setUrl(QUrl("http://google.com")) 
        self.browser.urlChanged.connect(self.update_urlbar) 
        self.browser.loadFinished.connect(self.update_title) 
        self.setCentralWidget(self.browser) 
        self.status = QStatusBar() 
        self.setStatusBar(self.status) 
        navtb = QToolBar("Navigation") 
        self.addToolBar(navtb) 
        back_btn = QAction("Back", self) 
        back_btn.setStatusTip("Back to previous page") 
        back_btn.triggered.connect(self.browser.back) 
        navtb.addAction(back_btn) 
        next_btn = QAction("Forward", self) 
        next_btn.setStatusTip("Forward to next page") 
        next_btn.triggered.connect(self.browser.forward) 
        navtb.addAction(next_btn) 
        reload_btn = QAction("Reload", self) 
        reload_btn.setStatusTip("Reload page") 
        reload_btn.triggered.connect(self.browser.reload) 
        navtb.addAction(reload_btn) 
        home_btn = QAction("Home", self) 
        home_btn.setStatusTip("Go home") 
        home_btn.triggered.connect(self.navigate_home) 
        navtb.addAction(home_btn) 

        navtb.addSeparator() 
        self.urlbar = QLineEdit() 
        self.urlbar.returnPressed.connect(self.navigate_to_url) 

        navtb.addWidget(self.urlbar) 
        stop_btn = QAction("Stop", self) 
        stop_btn.setStatusTip("Stop loading current page") 

        stop_btn.triggered.connect(self.browser.stop) 
        navtb.addAction(stop_btn) 
        self.show()
        
    def update_title(self): 
        title = self.browser.page().title() 
        self.setWindowTitle("% s - My New Browser" % title)
        
    def navigate_home(self): 
        self.browser.setUrl(QUrl("http://www.google.com")) 

    def navigate_to_url(self): 
        q = QUrl(self.urlbar.text()) 
        if q.scheme() == "": 
            q.setScheme("http") 
        self.browser.setUrl(q) 

    def update_urlbar(self, q): 
        self.urlbar.setText(q.toString()) 
        self.urlbar.setCursorPosition(0) 

app = QApplication(sys.argv) 
app.setApplicationName("My New Browser")  
window = MainWindow() 
app.exec_() 

I don't have any issues in the code but I want to customize the context menu which appears when we right-clink in a page, I want to change its background color, and add a label with an icon on top, also I want to change the font.

The better solution is to subclass the web view and override contextMenuEvent .
If you also want to customize the colors of the menu, you should set the stylesheet on the browser, using the appropriate selectors.

class Browser(QWebEngineView):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setStyleSheet('''
            QMenu {
                background: lightBlue;
                color: orange;
            }
            QMenu::item:selected {
                background: lightGray;
            }
        ''')

    def contextMenuEvent(self, event):
        self.menu = self.page().createStandardContextMenu()
        self.menu.addAction('My action')
        self.menu.popup(event.globalPos())

class MainWindow(QMainWindow): 
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs) 

        self.browser = Browser()
        # ...

Note: For various reasons (probably related to the complex architecture behind the web view), using menu.exec_() might lead to consolle warnings and painting artifacts, so using popup() is better; setting the menu as an instance attribute is mandatory, otherwise the menu would be garbage collected right after popup (which is not blocking).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM