VolArt VolArt - 3 months ago 9
Python Question

Why don't work linkClicked, and works linkHovered PyQt4?

Here is some code. I created modal window, set easy html, and then I want to connect some method on click link. Signal "linkClicked" dont works , but link are loaded. And signal linkHovered works. Where is mistake? And how to include some method to "linkClicked"?

# -*- coding: utf-8 -*-
import sys

from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4 import QtWebKit

class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.resize(1000, 500)
self.menu_bar = self.menuBar()
self.menuEngine()
def menuEngine(self):
self.podmenu2 = self.menu_bar.addMenu("Help")
self.about = QtGui.QAction("About", self )
self.about.setIconVisibleInMenu(True)
self.podmenu2.addAction(self.about)
self.about.triggered.connect(self.aboutView)
def aboutView(self):
def clicks(url):
print("DDD")
print(url)
mod_window = QtGui.QWidget(self, QtCore.Qt.Window)
mod_window.setWindowTitle("About")
mod_window.resize(500, 332)
horLayout = QtGui.QHBoxLayout(mod_window)
localHtmls = QtWebKit.QWebView()

localHtmls.setHtml("""
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<div>
<br/>
Home page <a href="https://google.com">LINK</a>
</div>
</body>
</html>
""")
horLayout.addWidget(localHtmls)

localHtmls.linkClicked.connect(clicks)
#localHtmls.page().linkHovered.connect(clicks)
mod_window.show()

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
windows = MainWindow()
windows.show()
sys.exit(app.exec_())

Answer

From the documentation for linkClicked:

This signal is emitted whenever the user clicks on a link and the page's linkDelegationPolicy property is set to delegate the link handling for the specified url.

So try:

localHtmls.page().setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateAllLinks)

But note that this completely bypasses the normal link-click handling, so you will have to implement that yourself (e.g. call load(url)).

Comments