Home > Back-end >  Get non url-link when clicking on Link in PyQt5 QWebEngineView
Get non url-link when clicking on Link in PyQt5 QWebEngineView

Time:08-16

I want to open self written html code in PyQt5 QWebEngineView. The html code should contain links, which are not url pages but have other information. Below a created a simple example. When clicking on the link named Link, python does not return the parameter inside the link. When I change the value of the link to a url, I get the QUrl as a result. Is there a way to get the content of the link (not a url) when clicked?

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
import sys

class MyWebEnginePage(QWebEnginePage):

    def acceptNavigationRequest(self, url,  _type, isMainFrame):
        if _type == QWebEnginePage.NavigationTypeLinkClicked:
            print(url)
            return False

        return super().acceptNavigationRequest(url,  _type, isMainFrame)

class App(QMainWindow):

    def __init__(self):
        super(App, self).__init__()
        browser = QWebEngineView()
        browser.setPage(MyWebEnginePage(self))
        link = "Hallo Welt"
        text = f'<a style="text-decoration:none;" href="{link}">Link</a>'
        browser.setHtml(text)
        self.setCentralWidget(browser)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = App()
    window.setGeometry(800, 100, 1000, 800)
    window.show()
    sys.exit(app.exec_())

CodePudding user response:

You can use a url with the data: scheme to send the link value. When the url is received, you can then check that it has the correct scheme, and emit a signal with only the url's path (which does not include the scheme).

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
import sys

class MyWebEnginePage(QWebEnginePage):
    dataLinkClicked = pyqtSignal(str)

    def acceptNavigationRequest(self, url,  _type, isMainFrame):
        if (_type == QWebEnginePage.NavigationTypeLinkClicked and
            url.scheme() == 'data'):
            # send only the url path
            self.dataLinkClicked.emit(url.path())
            return False
        return super().acceptNavigationRequest(url,  _type, isMainFrame)

class App(QMainWindow):
    def __init__(self):
        super(App, self).__init__()
        browser = QWebEngineView()
        page = MyWebEnginePage(self)
        # connect to the signal
        page.dataLinkClicked.connect(self.handleDataLink)
        browser.setPage(page)
        # use a data-url
        link = "data:Hallo Welt"
        text = f'<a style="text-decoration:none;" href="{link}">Link</a>'
        browser.setHtml(text)
        self.setCentralWidget(browser)

    def handleDataLink(self, text):
        print(text) # prints "Hallo Welt"

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = App()
    window.setGeometry(800, 100, 1000, 800)
    window.show()
    sys.exit(app.exec_())
  • Related