[SOLVED]addToJavaScriptWindowObject dosn´t work in Html5ApplicationViewer but in QWebView



  • Hi,

    i have a problem with addToJavaScriptWindowObject.

    I started a project as HTML5 Application and then i tried to connect Javascript with Qt like this:
    @#include <QApplication>
    #include "html5applicationviewer.h"
    #include "javascriptcontroller.h"
    #include <QGraphicsWebView>
    #include <QWebFrame>
    #include <QtWebKit>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    Html5ApplicationViewer viewer;
    viewer.setOrientation(Html5ApplicationViewer::ScreenOrientationAuto);
    viewer.showExpanded();
    viewer.loadFile&#40;QLatin1String("html/index.html"&#41;&#41;;
    viewer.webView()->page()->mainFrame()->addToJavaScriptWindowObject("Qt", new JavaScriptController);
    

    }@

    but it dosnt work. The JavaScript dosnt find the method returing the message:

    Type Error: undefined is not a constructor(evaluating Qt.test)

    But running the same Code in a Qt Application with a QWebView works

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QWebFrame>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    DataBaseController *dbc = new DataBaseController;
    dbc->setDataBase("Lop.db3");
    ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("Qt", new JavaScriptController);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    Here is the JavaScriptController.h

    @#ifndef JAVASCRIPTCONTROLLER_H
    #define JAVASCRIPTCONTROLLER_H

    #include <QObject>

    class JavaScriptController : public QObject
    {
    Q_OBJECT
    public:
    explicit JavaScriptController(QObject *parent = 0);

    signals:

    public slots:

    Q_INVOKABLE QString test();
    

    // Q_INVOKABLE QString queryVehicleBrand(QString jsonString);

    };

    #endif // JAVASCRIPTCONTROLLER_H@

    and the .cpp

    @#include "javascriptcontroller.h"
    #include <QMessageBox>

    JavaScriptController::JavaScriptController(QObject *parent) :
    QObject(parent)
    {
    }

    }

    QString JavaScriptController::test(){
    QMessageBox msg;
    msg.setText("WTF IT WORKS!?");
    msg.exec();
    }
    @

    and this is the snippet from JavaScript

    @function qtQueryVehicleBrand(request) {
    try{
    Qt.test();
    }catch(err){
    alert(err)
    }
    return '{"status":"ERR","message":"HaHa"}';@

    Does anyone see what i´m doing wrong in the HTML5 Application?



  • Well I solved this problem after a few sleepless nights by my own. I still don't know why it doesn't need the connect method in the QWebview but in the Html5ApplicationViewer. Anyway, all what count is that it works now^^

    I had to add in my JavaScriptController

    @JavaScriptController::JavaScriptController(QObject *parent, QWebFrame *_webFrame) :
    QObject(parent)
    {
    webFrame = _webFrame;
    QObject::connect(webFrame,SIGNAL(javaScriptWindowObjectCleared()), SLOT(addToJavaScript()));

    }

    void JavaScriptController::addToJavaScript() {
    webFrame->addToJavaScriptWindowObject("MyQtJavaScriptController", this);
    }@


Log in to reply
 

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