Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. [SOLVED]addToJavaScriptWindowObject dosn´t work in Html5ApplicationViewer but in QWebView
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Qt WebKit
2 Posts 1 Posters 2.0k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    Kleisophabo
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kleisophabo
      wrote on last edited by
      #2

      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);
      }@

      1 Reply Last reply
      0

      • Login

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