Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Regarding Qt bridge javascript
Forum Update on Monday, May 27th 2025

Regarding Qt bridge javascript

Scheduled Pinned Locked Moved General and Desktop
javascriptqt bridge
1 Posts 1 Posters 559 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neotrix
    wrote on 2 Jul 2015, 07:05 last edited by
    #1

    I am new to QT. Trying to use QT bridge to call c++ API from javascript. Have added a QWebView using QT GUI. When I launch the app and click on the html input Qt callback is not called. Constructer msg for MyJavaScriptOperations is printed when app is launched. So I believe the bridge object has been successfully created. I do see that the alert message in html code (see code) is called. Also, when I move the alert message after "myoperations.display(6)" call, alert msg is not shown. I must mention here, similar code works when the webview object is created in main.cpp. It doesn't work when the webview is created in qt creator GUI.

    Am I doing something wrong here? Appreciate your help.

    Important code snippets are as follows.

    main.cpp

    *QApplication a(argc, argv);
    MainWindow win;

    win.setupWeb();
    win.loadPage("http://localhost/test.html");
    win.show();
    return(a.exec);*

    MainWindow.cpp

    *class MyJavaScriptOperations : public QObject {
    Q_OBJECT
    public:
    MyJavaScriptOperations(){

    qDebug() << "In constructor";
    display(5);
    

    }

    public slots:

    Q_INVOKABLE void display(int index){

    qDebug() << "In display method "<< index;
    

    }

    };

    void MainWindow::setupWeb()
    {
    ui->webView->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
    }

    void MainWindow::loadPage(QString page)
    {
    ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("myoperations", new MyJavaScriptOperations);
    ui->webView->load(QUrl(page));
    }*

    test.html

    *<html>
    <body>
    <script type="text/javascript">

    function displayMessage(){
    alert("Hello display");
    myoperations.display(6);
    }
    </script>

    <p>Test Example to call Qt function from Javascript </p>

    <input type="button" value="Click me!" onclick="displayMessage()">

    </body>
    </html> *

    1 Reply Last reply
    0

    1/1

    2 Jul 2015, 07:05

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved