Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. Problem with addToJavaScriptWindowObject and JS throwing errors
QtWS25 Last Chance

Problem with addToJavaScriptWindowObject and JS throwing errors

Scheduled Pinned Locked Moved Qt WebKit
2 Posts 2 Posters 3.7k 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
    tdh_andy
    wrote on last edited by
    #1

    Hello,
    I'm trying to add errors thrown from my cpp objects exposed to JS.
    Here's an example

    @JSObject::JSObject(QWebFrame *p_frame, QObject *parent) :
    QObject(parent)
    {
    frame = p_frame;
    this->setObjectName("MyAPI");
    AttachObject();
    connect( frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(AttachObject()) );
    connect(this,SIGNAL(MyError()),this,SLOT(HandleMyError()));
    }
    QString JSObject::TestMethod()
    {
    //logic here, let's assume something went wrong
    qDebug("test method called, emmiting signal");
    emit MyError();
    qDebug("emit finished, method returning now");
    return "method finish";
    }

    void JSObject::AttachObject()
    {
    this->frame->addToJavaScriptWindowObject(this->objectName(),this);
    }
    void JSObject::HandleMyError()
    {
    QString l_js ="alert('throwing error'); throw new Error();";
    qDebug("evaluating javascript throw error");
    frame->evaluateJavaScript(l_js);
    qDebug("throw error call finished");
    return;
    }@

    and the html:
    @<html>
    <head>
    <title> Error throwing test</title>
    [removed]

    function test()
    {
    try
    {
    var result = MyAPI.TestMethod(); //this should throw an error
    alert(result);
    }
    catch(e)
    {
    alert("error cought");
    }
    return;
    }
    [removed]
    </head>
    <body>
    <button>Test</button>
    </body>

    </html>@
    The alert set here:
    @QString l_js ="alert('throwing error'); throw new Error();"; @
    happens when expected, i get this output:
    test method called, emmiting signal
    evaluating javascript throw error
    //alert happens here, untill i press ok there is no more output, execution stops and waits for alert window to close
    throw error call finished
    emit finished, method returning now

    So I know throw error is executed at the right time, but something is wrong, JS try/catch does not catch it.
    Anybody knows of a good way to add JS errors to exposed cpp objects?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jampy81
      wrote on last edited by
      #2

      I think you basically have the same problem as I do ("see here":http://developer.qt.nokia.com/forums/viewthread/6881/ ).

      The eval() trick does not work (I guess) because errors thrown inside the eval() are not passed to the calling JavaScript context.

      If perhaps you did find a solution in the meantime, then please let me know, thanks!

      1 Reply Last reply
      0

      • Login

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