Qt QWEBview JavaScript callback

丶灬走出姿态 提交于 2019-11-27 04:19:47

问题


How to pass a function "pointer" from JavaScript to a slot?

in JavaScript:

function f1()
{
    alert("f1");
}
qtclass.submit(f1);

and in Qt:

public slots:
    void submit(void * ptr) 
    {
        (void)ptr;
    }

I need the "f1", function to get fired in the JavaScript from the c++, once some processing is done. Also I do not know in advance the name of the function pointer.


回答1:


you should be able to execute your script using QWebFrame::evaluateJavaScript method. See if an example below would work for you:

initializing webview:

QWebView *view = new QWebView(this->centralWidget());
view->load(QUrl("file:///home//test.html"));
connect(view, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));

loadFinished signal handler:

void MyTestWindow::loadFinished(bool)
{
    QVariant f1result = ((QWebView*)sender())->page()->mainFrame()->evaluateJavaScript("f1('test param')");
    qDebug() << f1result.toString();
}

test.html:

<head>
    <script LANGUAGE="JavaScript">
        function f1 (s) 
        {
            alert (s) 
            return "f1 result"
        }
    </script>
</head>
<body>
    test html
</body>

evaluateJavaScript should trigger an alert message box and return QVariant with f1 function result.

hope this helps, regards




回答2:


You could solve this in another way, by using Qt signals, as follows:

class qtclass
{
   signals:
      void done(QString message);
};

In your HTML file you can connect to this signal, like this:

qtclass.done.connect(f1);

function f1(message)
{
   alert('f1 called from qtclass with message' + message);
}

Then you C++ class does not need to know about the javascript function.




回答3:


While it wouldn't work in all cases, you could simply pass a string to your slot. Your slot could then use evaluateJavaScript (as serge has suggested) to call the function.

function f1()
{
    alert("f1");
}
qtclass.submit("f1");

and in Qt:

public slots:
    void submit(QString functionName) 
    {
        m_pWebView->page()->mainFrame()->evaluateJavaScript(functionName + "()");
    }



回答4:


I have one entire working solution here - using slots. However, I couldn't get the signals working as suggested by Kurt.

#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 changes are -

<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>


来源:https://stackoverflow.com/questions/4975658/qt-qwebview-javascript-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!