qt4 - Qt 4.6 Adding objects and sub-objects to QWebView window object (C++ & Javascript) -
i working qt's qwebview, , have been finding lots of great uses adding webkit window object.
one thing nested objects... instance:
in javascript can...
var api = new object; api.os = new object; api.os.foo = function(){} api.window = new object(); api.window.bar = function(){}
obviously in cases done through more oo js-framework.
this results in tidy structure of:
>>>api ------------------------------------------------------- - api object {os=object, more... } - os object {} foo function() - win object {} bar function() -------------------------------------------------------
right i'm able extend window object of qtc++ methods , signals need, have 'seem' have in root child of "window". forcing me write js wrapper object hierarchy want in dom.
>>>api ------------------------------------------------------- - api object {os=function, more... } - os_foo function() - win_bar function() -------------------------------------------------------
this pretty simplified example... want objects parameters, etc...
does know of way pass child object object extends webframe's window object?
here's example code of how i'm adding 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 , bar getting band 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, want's it's bar back"; };
example.htm
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>stackoverflow question</title> <script type='text/javascript' src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script> </head> <body> <button type="button" onclick="api.os_foo()">foo</button> <button type="button" onclick="api.win_bar()">bar</button> </body> </html>
i'm reasonably new c++ programming (coming web , python background).
hopefully example serve not other new users, interesting more experienced c++ programmer elaborate on.
thanks assistance can provided. :)
i had same issue, , found answer here: http://doc.qt.nokia.com/4.7/qtwebkit-bridge.html
accessing child qobjects
every named child of qobject
(that is, qobject::objectname()
not empty string) default available property of javascript wrapper object. example, if have qdialog child widget objectname property "okbutton", can access object in script code through expression
mydialog.okbutton
since objectname q_property
, can manipulate name in script code to, example, rename object:
mydialog.okbutton mydialog.okbutton.objectname = "cancelbutton"; // on, mydialog.cancelbutton references button
Comments
Post a Comment