开发者

Qt 4.6 Adding objects and sub-objects to QWebView window object (C++ & Javascript)

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()
-----------------------------开发者_StackOverflow中文版--------------------------

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";
};

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 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. :)


I had the same issue, and found an answer here: http://doc.qt.nokia.com/4.7/qtwebkit-bridge.html

Accessing Child QObjects

Every named child of the QObject (that is, for which QObject::objectName() is not an empty string) is by default available as a property of the JavaScript wrapper object. For example, if you have a QDialog with a child widget whose objectName property is "okButton", you can access this object in script code through the expression

 myDialog.okButton

Since objectName is itself a Q_PROPERTY, you can manipulate the name in script code to, for example, rename an object:

 myDialog.okButton
 myDialog.okButton.objectName = "cancelButton";
 // from now on, myDialog.cancelButton references the button
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜