Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window does not open new window or tab on external url link click

I need to open an URL using pyQt5. The page has several links that open a new window. pyQt5 opens a windows for the URL but does not do anything after clicking on a link that should open a new window. P.S I'm using pyQt5.6

I have tried it on Linux centOs but nothing works.

from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage


class WebEnginePage(QWebEnginePage):
    def acceptNavigationRequest(self, url,  _type, isMainFrame):
        if _type == QWebEnginePage.NavigationTypeLinkClicked:
            return True
        return QWebEnginePage.acceptNavigationRequest(self, url,  _type,      isMainFrame)

class HtmlView(QWebEngineView):
    def __init__(self, *args, **kwargs):
        QWebEngineView.__init__(self, *args, **kwargs)
        self.setPage(WebEnginePage(self))

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    w = HtmlView()
    w.load(QUrl("https://gmail.com"));
    w.show()
    sys.exit(app.exec_())

I expect it to open a new window on click of target='_blank' on any webpage.

like image 714
rajesh kapoor Avatar asked Oct 26 '25 08:10

rajesh kapoor


1 Answers

You have to override the createWindow method and return a QWebEngineView, but for the object not to be distruded it must be the child of another window or be part of a container that has a longer life cycle.

from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets

class WebEnginePage(QtWebEngineWidgets.QWebEnginePage):
    def acceptNavigationRequest(self, url,  _type, isMainFrame):
        if _type == QtWebEngineWidgets.QWebEnginePage.NavigationTypeLinkClicked:
            return True
        return super(WebEnginePage, self).acceptNavigationRequest(url, _type, isMainFrame)

class HtmlView(QtWebEngineWidgets.QWebEngineView):
    def __init__(self, windows, *args, **kwargs):
        super(HtmlView, self).__init__(*args, **kwargs)
        self.setPage(WebEnginePage(self))
        self._windows = windows
        self._windows.append(self)

    def createWindow(self, _type):
        if QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
            v = HtmlView(self._windows)
            v.resize(640, 480)
            v.show()
            return v

if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    windows = []
    w = HtmlView(windows)
    w.load(QtCore.QUrl("https://gmail.com"));
    w.show()
    sys.exit(app.exec_())
like image 109
eyllanesc Avatar answered Oct 28 '25 22:10

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!