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) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    this->setObjectName("MyWin");
    connectCPPObjectToJS(this->objectName() , this);
    loadUrl(QCoreApplication::applicationDirPath() + "/sura.htm");

    }

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

    void MainWindow::loadUrl(const QString & localpath)
    {
    ui->webView->load(QUrl::fromLocalFile(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 :
    @
    <html>

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

    [removed]

    </head>

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

    </html>
    @

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.