QWebEngineView and QWebChannel

Hi,

I am having problem getting a simple implementation of QWebChannel between python and QWebEngineView working, what am I missing here (I posted the same over at the Qt forums but the traffic there seems very low)?

import sys
from PySide6.QtCore import QObject
from PySide6.QtWebChannel import QWebChannel
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtWidgets import QApplication

class Py(QObject):
    def msg(self, msg):
        print("msg: ", msg)

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>
                var py
                channel = new QWebChannel(qt.webChannelTransport, function(channel) {
                    py = channel.objects.py
                })
            </script>
        </head>
        <body>
            Demo<br>
            <button onclick="py.msg('button clicked')">click me</button>
        </body>
    </html>
    ''')
    web.show()
    app.exec()
```

Cheers

Not sure what the problem is, but you can see Anki’s webview.py for how we do it.

Thanks, I looked at webview.py but apparently misread it when following the code it as it does a lot of other things. For anyone looking for something minimal, here is a working solution (although when using aqt @Slot should be @pyqtSlot):

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 cmd(self, cmd):
        print("py.cmd: ", cmd)
        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 pycmd, bridgeCommand
                    new QWebChannel(qt.webChannelTransport, function(channel) {
                        pycmd = bridgeCommand = function (cmd, cb = Function.prototype) {
                            channel.objects.py.cmd(cmd, (res) => cb(JSON.parse(res)))
                            return false
                        }
                    })
                </script>
            </head>
            <body>
                <button onclick="pycmd('button clicked', (res) => {alert(res)})">click me</button>
            </body>
        </html>
    ''')
    web.show()
    app.exec()
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.