Problem evaluating javascript source with QWebView



  • Hi,

    I could use some help solving the following problem. I keep getting an error with the following code:
    // HtmlWizard.cpp
    QString scriptSource = "if (Wizard_OnNext != null)
    wizard.nextButtonClicked.connect(Wizard_OnNext);";
    ui.webView->page()->mainFrame()->evaluateJavaScript(scriptSource);

    Here I try to execute some javascriptcode with the function evaluateJavascript, it should never give an error. However when the javascript function Wizard_OnNext does not exist,
    Qt shows an alert with the error:
    "undefined:1: ReferenceError: Can't find variable: Wizard_OnNext"

    This is incorrect, since the syntax is correct. Does anybody know how I can solve this problem?

    Thanks in advance,

    Jelko



  • @Jelko Have you tried putting brackets around the connect line?



  • I try to use standard javascript code to determine if a function exists, it seems
    that the Qt engine which parses javascript does not recognize it as valid javascript code. Does somebody know a workaround?

    Thanks in advance



  • @raf924 I am not sure what you mean: I tried different combinations. Could you give an example?



  • @Jelko

    if (Wizard_OnNext != null) {
     wizard.nextButtonClicked.connect(Wizard_OnNext);
    }
    


  • It now works:

    QString scriptSource = "if (typeof Wizard_OnNext == 'function') { wizard.nextButtonClicked.connect(Wizard_OnNext); }";
    ui.webView->page()->mainFrame()->evaluateJavaScript(scriptSource);


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.