Qt QWEBview JavaScript 回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4975658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:32:43  来源:igfitidea点击:

Qt QWEBview JavaScript callback

javascriptqtqwebviewslot

提问by user457015

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

如何将函数“指针”从 JavaScript 传递到插槽?

in JavaScript:

在 JavaScript 中:

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

and in Qt:

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

一旦完成一些处理,我需要“f1”函数从 c++ 在 JavaScript 中被触发。另外我事先不知道函数指针的名称。

采纳答案by serge_gubenko

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

您应该能够使用QWebFrame::evaluateJavaScript方法执行您的脚本。看看下面的例子是否适合你:

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:

loadFinished 信号处理程序:

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

test.html:

测试.html:

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

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

evaluateJavaScript应该触发一个警告消息框并返回带有 f1 函数结果的 QVariant。

hope this helps, regards

希望这有帮助,问候

回答by Kurt Pattyn

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

您可以通过使用 Qt 信号以另一种方式解决此问题,如下所示:

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

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

在您的 HTML 文件中,您可以连接到此信号,如下所示:

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.

那么你的 C++ 类不需要了解 javascript 函数。

回答by Noah Callaway

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.

虽然它不会在所有情况下都有效,但您可以简单地将字符串传递给您的插槽。然后您的插槽可以使用evaluateJavaScript(如serge 建议的那样)来调用该函数。

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

and in Qt:

在 Qt 中:

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

回答by dexterous

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 -

html 文件更改为 -

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