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_())