Results 1 to 4 of 4

Thread: Difficulties accessing object in JavaScript using QWebChannel in other scope

  1. #1
    Join Date
    May 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Difficulties accessing object in JavaScript using QWebChannel in other scope

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Difficulties accessing object in JavaScript using QWebChannel in other scope

    Maybe the JS object created by new QWebChannel(...) needs to be saved somewhere as well?
    E.g. maybe it is currently garbage collected by the JS engine after the callback ran.

    Cheers,
    _

  3. #3
    Join Date
    May 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Difficulties accessing object in JavaScript using QWebChannel in other scope

    Thank you for the reply!

    Maybe the JS object created by new QWebChannel(...) needs to be saved somewhere as well?
    E.g. maybe it is currently garbage collected by the JS engine after the callback ran.
    Do you mean doing something like:

    Qt Code:
    1. window.webChannel = 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. });
    To copy to clipboard, switch view to plain text mode 

    Unfortunately, that doesn't seem to fix the problem either.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Difficulties accessing object in JavaScript using QWebChannel in other scope

    Hmm, yes, that's what I had in mind.
    All examples seem to indicate that this and your original approach should work.

    Does the object still exist on the application side?

    Cheers,
    _

Similar Threads

  1. Accessing Javascript function from QT/C++
    By scgrant327 in forum Qt Quick
    Replies: 7
    Last Post: 8th February 2016, 21:35
  2. Where is qwebchannel.js?
    By RolandHughes in forum Qt Programming
    Replies: 1
    Last Post: 7th June 2015, 11:42
  3. SIGNAL/SLOT - Object scope?
    By StarShaper in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2012, 14:35
  4. Replies: 9
    Last Post: 29th November 2010, 13:12
  5. WebKit - accessing javascript results + googleMap
    By giusepped in forum Qt Programming
    Replies: 10
    Last Post: 30th May 2009, 01:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.