在QtWebkit中,如何安装从C ++到Javaobject窗口的回调? [英] In QtWebkit, how to install the callback from C++ to Javaobject window?

查看:229
本文介绍了在QtWebkit中,如何安装从C ++到Javaobject窗口的回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了HTML-JS,这使用 QtWebkit 从JS调用C ++方法。我能够成功。现在,我想从C ++方法发送一个回调到JavaScript窗口。

 这是我的代码。

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

class MyJavaScriptOperations:public QObject {
Q_OBJECT
public:
Q_INVOKABLE qint32 MultOfNumbers(int a,int b){
qDebug()< ; a * b;
return(a * b);
}
};

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

QWebView * view = new QWebView();
view-> resize(400,500);
view-> page() - > mainFrame() - > addToJavaScriptWindowObject(myoperations,new MyJavaScriptOperations);
view-> load(QUrl(./ shreyas.html));
view-> show();

return a.exec();
}
#includemain.moc

 < html> 
< head>
< script>


函数Multiply()
{
var result = myoperations.MultOfNumbers(document.forms [DEMO_FORM] [Multiplicant_A]。 forms [DEMO_FORM] [Multiplicant_B]。value);
document.getElementById(answer)。value = result;

}

< / script>
< / head>

< body>
< form name =DEMO_FORM>
乘数A:< input type =numbername =Multiplicant_A>< br>
乘数B:< input type =numbername =Multiplicant_B>< br>
结果:< input type =numberid =answername =Multiplicant_C>< br>
< input type =buttonvalue =Multiplication_compute_on_C ++onclick =Multiply()>
< / form>
< / body>
< / html>


解决方案

我不知道你能不能调用JS回调直接,但在一个捏,你可以使用你的主框架的 evaluateJavascript ()方法从C ++调用一些JS。



或者你可以连接到JavaScript端的信号,参见 Qt QWEBview JavaScript回调一些想法。



对连接的一个很好的概述机制也可以在 Qt文档中找到。 p>

I have implemented the HTML-JS, this calls the C++ method from the JS using QtWebkit. I am able to do it successfully. Now, I want to send a callback to JavaScript window from the C++ method. How can I 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:
    Q_INVOKABLE qint32 MultOfNumbers(int a, int b) {
        qDebug() << a * b;
        return (a*b);
    }
};

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

    QWebView *view = new QWebView();
    view->resize(400, 500);
    view->page()->mainFrame()->addToJavaScriptWindowObject("myoperations", new MyJavaScriptOperations);
    view->load(QUrl("./shreyas.html"));
    view->show();

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

The Java script is here.

<html>
<head>
<script>


function Multiply()
{
   var result = myoperations.MultOfNumbers(document.forms["DEMO_FORM"]["Multiplicant_A"].value, document.forms["DEMO_FORM"]["Multiplicant_B"].value);
   document.getElementById("answer").value = result;

}

</script>
</head>

<body>
<form name="DEMO_FORM">
Multiplicant A: <input type="number" name="Multiplicant_A"><br>
Multiplicant B: <input type="number" name="Multiplicant_B"><br>
Result : <input type="number" id="answer" name="Multiplicant_C"><br>
<input type="button" value="Multiplication_compute_on_C++" onclick="Multiply()">
</form>
</body>
</html> 

解决方案

I don't know if you can somehow call a JS callback directly, but in a pinch you could use your main frame's evaluateJavascript() method to call some JS from C++.

Or you can connect to signals from the JavaScript side, see Qt QWEBview JavaScript callback for some ideas.

A good overview over the connection mechanisms can also be found in the Qt docs

这篇关于在QtWebkit中,如何安装从C ++到Javaobject窗口的回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆