Issue
Is it possible to use one of the Web classes in PySide to load a local html/JS file which contains buttons, and have those buttons connected to a PySide slot?
Solution
You can use QtWebKit to export a QObject to the JavaScript context and then cast the signal from JavaScript as shown in the following example:
├── index.html
└── main.py
main.py
import os
from PySide import QtCore, QtGui, QtWebKit
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
class ClickListener(QtCore.QObject):
clicked = QtCore.pyqtSignal()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
listener = ClickListener()
listener.clicked.connect(lambda: print("clicked"))
filename = os.path.join(CURRENT_DIR, "index.html")
w = QtWebKit.QWebView()
w.page().mainFrame().addToJavaScriptWindowObject("qt_clicked_listener", listener)
w.load(QtCore.QUrl.fromLocalFile(filename))
w.show()
sys.exit(app.exec_())
index.html
<!DOCTYPE html>
<html>
<body>
<button id="myBtn">Click me</button>
<script>
document.getElementById("myBtn").addEventListener("click", function() {
qt_clicked_listener.clicked();
});
</script>
</body>
</html>
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.