Please nominate your Qt Champions for 2021!

Connect qwebkit and javascript problem

  • hi to all
    i am using qwebkit
    i have a cpp object and export it to javascript but it does not connect to it , because it does not work
    here is my code :
    my cpp code :
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QWebFrame>
    #include <QMessageBox>

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)
    connectCPPObjectToJS(this->objectName() , this);
    loadUrl(QCoreApplication::applicationDirPath() + "/sura.htm");


    void MainWindow::test()
    QMessageBox::information(this , "a" , "hello");

    void MainWindow::loadUrl(const QString & localpath)

    void MainWindow::on_comboBox_currentIndexChanged(int index)
    QString s ;
    QMessageBox::information(this , "index change" , s.setNum(index));
    emit sendChangedIndexToJS(index);


    void MainWindow::connectCPPObjectToJS(const QString objectname , QObject* objecttype)
    ui->webView->page()->mainFrame()->addToJavaScriptWindowObject( objectName() , objecttype);


    void MainWindow::on_comboBox_textChanged(QString a)
    QMessageBox::information(this , "text change" , a);



    and here is my html code :

    <meta http-equiv="Content-Language" content="en-us">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>مثال چهار قل</title>
    function updatesura(value){
    alert("the value was changed to " + value);



    <p align="center"><font face="Tahoma">سوره</font>


    what is wrong ?
    thanks a lot

  • i put the statement javascript "MyWin.test();" in a try catch and the error is that :
    ReferenceError : can't find variable MyWin

    so now i am sure that the connection is wrong . but i do not know why:(

  • Quoting the docs:

    bq. If you want to ensure that your QObjects remain accessible after loading a new URL, you should add them in a slot connected to the javaScriptWindowObjectCleared() signal.

  • thanks a lot . you are right

Log in to reply