1

I have a Qt project with a Webview which is loading a HTML file which is Javascriptfiles. Now I want to call a function on Qt side via JavaScript.

So, having the following Qt function:

void MainWIndow::myFunction(angle)
{
    qDebug() << angle;
}

I want to call it from JavaScript side:

function angleChanged(angle){
    Qt.MainWIndow::myFunction(angle);
}

How can I do this?

1 Answer 1

1

If Qt is your JavaScriptWindowObject, you should use:

In your Qt code behind (MainWIndow class):

MainWIndow.h file:

public slots:
    void populateJavaScriptWindowObject();
    void myFunction(int angle);

MainWIndow.cppfile:

//in constructor
connect(ui->webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
         this, SLOT(populateJavaScriptWindowObject()));

//in populateJavaScriptWindowObject SLOT
void MainWindow::populateJavaScriptWindowObject()
{
    QWebFrame *frame = ui->webView->page()->mainFrame();
    frame->addToJavaScriptWindowObject("Qt", this);
}

In your Javascript code:

function angleChanged(angle) {
    Qt.myFunction(angle);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you.How I can make QT to JavaScriptWindowObject
using addToJavaScriptWindowObject() method in populateJavaScriptWindowObject().
Thx will try it soon as possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.