@TRIAEIOU
Solved, I was missing @Slot:
import sys, json
from PySide6.QtCore import QObject, Slot
from PySide6.QtWebChannel import QWebChannel
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtWidgets import QApplication
class Py(QObject):
@Slot(str, result=str)
def msg(self, msg):
print("py.msg: ", msg)
return json.dumps("clicked")
if __name__ == "__main__":
app = QApplication(sys.argv)
web = QWebEngineView()
channel = QWebChannel(web)
py = Py(app)
channel.registerObject("py", py)
web.page().setWebChannel(channel)
web.setHtml('''
<html>
<head>
<script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>
<script type="text/javascript">
var pymsg
new QWebChannel(qt.webChannelTransport, function(channel) {
pymsg = function (msg, cb = Function.prototype) {
channel.objects.py.msg(msg, (res) => cb(JSON.parse(res)))
return false
}
})
</script>
</head>
<body>
<button onclick="pymsg('button clicked', (res) => {alert(res)})">click me</button>
</body>
</html>
''')
web.show()
app.exec()