I am working with Qt's QWebView, and have been finding lots of great uses for adding to the webkit window object.
One thing I would like to do is nested objects... for instance:
in Javascript I can...
var api = new Object;
api.os = new Object;
api.os.foo = function(){}
api.window = new Object();
api.window.bar = function(){}
obviously in most cases this would be done through a more OO js-framework.
This results in a tidy structure of:
>>>api
-------------------------------------------------------
- api Object {os=Object, more... }
- os Object {}
foo function()
- win Object {}
bar function()
-------------------------------------------------------
Right now I'm able to extend the window object with all of the qtC++ methods and signals I need, but they all have 'seem' to have to be in a root child of "window". This is forcing me to write a js wrapper object to get the hierarchy that I want in the DOM.
>>>api
-------------------------------------------------------
- api Object {os=function, more... }
- os_foo function()
- win_bar function()
-------------------------------------------------------
This is a pretty simplified example... I want objects for parameters, etc...
Does anyone know of a way to pass an child object with the object that extends the WebFrame's window object?
Here's some example code of how I'm adding the object:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QWebFrame>
#include "mainwindow.h"
#include "happyapi.h"
class QWebView;
class QWebFrame;
QT_BEGIN_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
private slots:
void attachWindowObject();
void bluesBros();
private:
QWebView *view;
HappyApi *api;
QWebFrame *frame;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include <QDebug>
#include <QtGui>
#include <QWebView>
#include <QWebPage>
#include "mainwindow.h"
#include "happyapi.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
view = new QWebView(this);
view->load(QUrl("file:///Q:/example.htm"));
api = new HappyApi(this);
QWebPage *page = view->page();
frame = page->mainFrame();
attachWindowObject();
connect(frame,
SIGNAL(javaScriptWindowObjectCleared()),
this, SLOT(attachWindowObject()));
connect(api,
SIGNAL(win_bar()),
this, SLOT(bluesBros()));
setCentralWidget(view);
};
void MainWindow::attachWindowObject()
{
frame->addToJavaScriptWindowObject(QString("api"), api);
};
void MainWindow::bluesBros()
{
qDebug() << "foo and bar are getting the band back together!";
};
happyapi.h
#ifndef HAPPYAPI_H
#define HAPPYAPI_H
#include <QObject>
class HappyApi : public QObject
{
Q_OBJECT
public:
HappyApi(QObject *parent);
public slots:
void os_foo();
signals:
void win_bar();
};
#endif // HAPPYAPI_H
happyapi.cpp
#include <QDebug>
#include "happyapi.h"
HappyApi::HappyApi(QObject *parent) :
QObject(parent)
{
};
void HappyApi::os_foo()
{
qDebug() << "foo called, it want's it's bar back";
};
I'm reasonably new to C++ programming (coming from a web and python background).
Hopefully this example will serve to not only help other new users, but be something interesting for a more experienced c++ programmer to elaborate on.
Thanks for any assistance that can be provided. :)