QT-Qweb Callback not working in the example here
Asked Answered
L

1

0

Now this code works with the slot mechanism. However, I want to try out the signal way also. However, I am unable to do so? Any more ideas on it?

I want to call function f1 of the Javascript from the QT. However, I am unable to do so. I don't see the callback being received by the JS f1(). I have followed the earlier post on it, Qt QWEBview JavaScript callback . However, I am unable to do so. Here is my code.

#include <QtGui/QApplication>
#include <QApplication>
#include <QDebug>
#include <QWebFrame>
#include <QWebPage>
#include <QWebView>

class MyJavaScriptOperations : public QObject {
    Q_OBJECT
public:
     QWebView *view;
     MyJavaScriptOperations();

    Q_INVOKABLE qint32 MultOfNumbers(int a, int b) {
        qDebug() << a * b;
        return (a*b);
    }
public slots:
     void callback();
public:

void  firecb();

 signals:
      void done();
};


MyJavaScriptOperations::MyJavaScriptOperations()
{
    view = new QWebView();
    view->resize(400, 500);

    connect(view->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(callback()));

    view->load(QUrl("./shreyas.html"));

    view->show();


    qDebug()<<view;


}

void MyJavaScriptOperations::callback()
{
    qDebug()<<"Sending hello text";
    QString function = "f1()";
    view->page()->mainFrame()->addToJavaScriptWindowObject("myoperations", this);
    //view->page()->mainFrame()->evaluateJavaScript("f1()");
    done();
}

void  MyJavaScriptOperations::firecb()
{
     qDebug()<<"Emitting Signal";
     done();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    MyJavaScriptOperations *jvs = new MyJavaScriptOperations;
    jvs->firecb();

    return a.exec();
}
#include "main.moc"

The html file.

<head>
    <script LANGUAGE="JavaScript">


function f1()
{
   alert('f1 called from qtclass with message');
   document.write("HELLLLLLLLL");
}
myoperations.callback(f1);


function f2()
{
   var result = myoperations.MultOfNumbers(3,7);
   document.write(result);
    alert('f1 called from qtclass with message');
}


function f3()
{

    alert('f3');
}

myoperations.done.connect(f3);
    </script>
</head>
<body>
    test html
<input type="button" value="click" onclick="f2()">
</body>
Labdanum answered 26/2, 2014 at 10:0 Comment(0)
S
1

You have to inject your QObject calling addToJavaScriptWindowObject() in a slot invoked by QWebFrame::javaScriptWindowObjectCleared() signal, elsewhere loading an URL will clear all JS objects previously injected. See documentation on addToJavaScriptWindowObject.

Speculation answered 26/2, 2014 at 10:8 Comment(9)
It didn't work for me. Can I load my new program again?Labdanum
I do not see you perform connection jvs->view()->page->mainFrame() SIGNAL(javaScriptWindowObjectCleared()) to slot that will inject your QObject into the JavaScript.Speculation
Do you find any other problem now? I have updated the code now.Labdanum
view->page()->mainFrame()->addToJavaScriptWindowObject("myoperations", this) must be called in your callback() slot in this case. You are still calling it outside the javaScriptWindowObjectCleared signal.Speculation
I tried that earlier also, but no luck. However, I have updated my program again. If you feel something is wrong. Is something wrong with JS or view->page()->mainFrame()->evaluateJavaScript("f1()");?Labdanum
You have several problems with your HTML file. You cannot use MyJavaScriptOperations.callback("f1"); but myoperations.callback("f1"). Now the callback slot does not expect any arguments and it will reinject the object, so I do not see what you're trying to achieve like this... Except this your code works fine, it shows me 21 when I click the button :)Speculation
Thanks! This worked. Now, any idea on Signals way of doing it. function f3(message) { alert('f3' + message); } myoperations.done.connect("f3"); In C++ code, I am doing the following - emit done("Shreyas"); Done is my signal - signals: void done(QString message);Labdanum
You do not use quotation mark for functions in JavaScript. Functions are 1st class citizens. Write myoperations.done.connect(f3) instead.Speculation
let us continue this discussion in chatLabdanum

© 2022 - 2024 — McMap. All rights reserved.