I'm planning on executing a javascript function from pyqt QWebEngine. I followed a example which was using a map and map bound were retrieved when a Qt application button was pushed, and wrote a small example.
html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script>
function helloWorld(param1, param2) {
return "Hello world " + param1 + " " + param2;
}
</script>
</body>
</html>
Python:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import QWebEngineView
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.form_widget = FormWidget(self)
_widget = QWidget()
_layout = QVBoxLayout(_widget)
_layout.addWidget(self.form_widget)
self.setCentralWidget(_widget)
class FormWidget(QWidget):
def __init__(self, parent):
super(FormWidget, self).__init__(parent)
self.__controls()
self.__layout()
self.browser.page().runJavaScript("helloWorld()", self.ready)
def __controls(self):
html = open('test.html', 'r').read()
self.browser = QWebEngineView()
self.browser.setHtml(html)
def __layout(self):
self.vbox = QVBoxLayout()
self.hBox = QVBoxLayout()
self.hBox.addWidget(self.browser)
self.vbox.addLayout(self.hBox)
self.setLayout(self.vbox)
def ready(self, returnValue):
print(returnValue)
def main():
app = QApplication(sys.argv)
win = MainWindow()
win.show()
app.exec_()
if __name__ == '__main__':
sys.exit(main())
Nevertheless, I get the following error:
js: Uncaught ReferenceError: helloWorld is not defined
None ----> This is the return value printed at ready()
What am I missing?