I'm new to QWebChannel, so I apologize in advance if this is a stupid question. But I am having some difficulty figuring out how to access Qt objects/methods, elsewhere in my JavaScript app, using QWebChannel. The comments in the JS snippet below should clarify what I mean. I can access everything fine in the callback when I instantiate QWebChannel, but it seems like channel.objects loses its context outside of that...even if I try to make it global.

JS snippet:

Qt Code:
  1. new QWebChannel(qt.webChannelTransport, function (channel) {
  2. channel.objects.bridge.sendMessage("hello"); // this works
  3. window.bridge = channel.objects.bridge; // try to make global
  4. });
  5.  
  6. function foo (msg) {
  7. bridge.sendMessage("hello from foo"); // this doesn't work
  8. window.bridge.sendMessage("hello from foo"); // this doesn't work
  9. // how might I access sendMessage here?
  10. }
To copy to clipboard, switch view to plain text mode 

On the Python side:

Qt Code:
  1. from PyQt5.QtWebEngineWidgets import QWebEnginePage
  2. from PyQt5.QtCore import pyqtSlot, pyqtSignal
  3.  
  4.  
  5. class MyWebPage(QWebEnginePage):
  6.  
  7. Bridge = pyqtSignal(list)
  8.  
  9. def __init__(self):
  10. super(MyWebPage, self).__init__()
  11.  
  12. def javaScriptConsoleMessage(self, level, message, lineNumber, sourceId):
  13. toWrite = "JsConsole(%s:%d):\n %s" % (sourceId, lineNumber, message)
  14. self._log.info(toWrite)
  15. print toWrite
  16.  
  17. @pyqtSlot(str)
  18. def sendMessage(self, msg):
  19. print msg
  20. self.Bridge.emit(msg) # over here, a signal is emitted with the message for elsewhere in the code
To copy to clipboard, switch view to plain text mode 

Any help would be greatly appreciated. I am using the qtwebchannel.js file included in Qt5.6 and just loading it into my HTML document.