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>
Community
  • 1
  • 1
dexterous
  • 6,422
  • 12
  • 51
  • 99

1 Answers1

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.

Archie
  • 2,644
  • 21
  • 21
  • It didn't work for me. Can I load my new program again? – dexterous Feb 26 '14 at 11:07
  • I do not see you perform connection jvs->view()->page->mainFrame() SIGNAL(javaScriptWindowObjectCleared()) to slot that will inject your QObject into the JavaScript. – Archie Feb 26 '14 at 14:00
  • Do you find any other problem now? I have updated the code now. – dexterous Feb 26 '14 at 14:50
  • 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. – Archie Feb 26 '14 at 15:00
  • 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()");? – dexterous Feb 26 '14 at 15:11
  • 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 :) – Archie Feb 26 '14 at 15:40
  • 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); – dexterous Feb 26 '14 at 16:13
  • You do not use quotation mark for functions in JavaScript. Functions are 1st class citizens. Write myoperations.done.connect(f3) instead. – Archie Feb 26 '14 at 16:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48498/discussion-between-shreyas-joshi-and-archie) – dexterous Feb 26 '14 at 16:30