Datadir Datadir Datadir

QWebEngineView: href in html file not working

72 Views Asked by At

I have a test.html file like:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <a style="padding-right:5px" target="_blank" href="data/">Datadir</a>
</body>
</html>

the directory include test.html is like:

test.html
data\
    a.txt
    b.txt

and I use "file:///remote/us01home19/ktc/public_html/testLocalHref/test.html" and click on Datadir in firefox and chrome, the result is like:

enter image description here

and:

enter image description here

I write a pyside2 code to do the same thing, the code is like:

from PySide2 import QtCore, QtWidgets, QtWebEngineWidgets

import os
import sys

class CustomWebEnginePage(QtWebEngineWidgets.QWebEnginePage):
    # Store second window.
    external_window = None

    def acceptNavigationRequest(self, url,  _type, isMainFrame):
        print(url, _type, isMainFrame)
        if _type == QtWebEngineWidgets.QWebEnginePage.NavigationTypeLinkClicked:
            if not self.external_window:
                self.external_window = QtWebEngineWidgets.QWebEngineView()

            self.external_window.setUrl(url)
            self.external_window.show()
            return False

        return super().acceptNavigationRequest(url,  _type, isMainFrame)
    
class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.browser = QtWebEngineWidgets.QWebEngineView()
        self.browser.setPage(CustomWebEnginePage(self))
        # self.browser.setUrl(QtCore.QUrl("https://code.visualstudio.com"))
        self.browser.setUrl(QtCore.QUrl("file:///remote/us01home19/ktc/public_html/testLocalHref/test.html"))
        # self.browser.setUrl(QtCore.QUrl("file:///remote/tw_rnd1/ktc/prog/python/pyside2/WebEngine/data/aaa.ava.summary.html"))
        #self.browser.setUrl(QtCore.QUrl("file:///remote/tw_rnd1/ktc/prog/python/pyside2/WebEngine/data/aaa_ava_corners/C_1"))
        self.setCentralWidget(self.browser)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    
    sys.exit(app.exec_())

But execute the code, I get the result like:

enter image description here

but click on the Datadir, there is no response, even QWebEnginePage.acceptNavigationRequest is not triggered. Is there a way to make the QWebEngine behavior like Firefox and Chrome?

I have try to add print message in QWebEnginePage.acceptNavigationRequest function to catch the click dataDir information, but it seems there is no response for this.

python version:python-3.9.0, os version : "CentOS Linux 7", Qt Version : "Qt_5.15" python and Qt are custom build

1

There are 1 best solutions below

1
柯德清 On

I find the problem comes from html target="_blank" not work in QWebEnginePage, it can avoid by implementing class function createWindow in QtWebEngineWidgets like:

class CustomWebEnginePage(QtWebEngineWidgets.QWebEnginePage):
# Store second window.
external_window = None

def acceptNavigationRequest(self, url,  _type, isMainFrame):
    print(url, _type, isMainFrame)
    if _type == QtWebEngineWidgets.QWebEnginePage.NavigationTypeLinkClicked:
        if not self.external_window:
            self.external_window = QtWebEngineWidgets.QWebEngineView()

        self.external_window.setUrl(url)
        self.external_window.show()
        return False

    return super().acceptNavigationRequest(url,  _type, isMainFrame)
def createWindow(self, t):
    return self