Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. Python QWebEngineView and QWebChannel
Forum Updated to NodeBB v4.3 + New Features

Python QWebEngineView and QWebChannel

Scheduled Pinned Locked Moved Solved QtWebEngine
2 Posts 1 Posters 711 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TRIAEIOU
    wrote on last edited by
    #1

    Hi, I can't get a minimal implementation of QWebChannel communication between a python script and QWebEngineView to work, what am I missing here?

    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()
    

    Thanks!

    T 1 Reply Last reply
    0
    • T TRIAEIOU

      Hi, I can't get a minimal implementation of QWebChannel communication between a python script and QWebEngineView to work, what am I missing here?

      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()
      

      Thanks!

      T Offline
      T Offline
      TRIAEIOU
      wrote on last edited by
      #2

      @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()
      
      1 Reply Last reply
      0
      • T TRIAEIOU has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved