Qt 4.6将对象和子对象添加到QWebView窗口对象(C ++和Javascript) [英] Qt 4.6 Adding objects and sub-objects to QWebView window object (C++ & Javascript)
问题描述
我正在使用Qt的QWebView,并且已经发现了许多用于添加到webkit窗口对象的很好的用途。
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:
在Javascript中我可以...
in Javascript I can...
var api = new Object;
api.os = new Object;
api.os.foo = function(){}
api.window = new Object();
api.window.bar = function(){}
这种情况可以通过更多的OO js框架完成。
这导致整洁的结构:
>>>api
-------------------------------------------------------
- api Object {os=Object, more... }
- os Object {}
foo function()
- win Object {}
bar function()
-------------------------------------------------------
现在我可以使用我需要的所有qtC ++方法和信号扩展窗口对象,但它们都似乎有了成为窗口的根子。这迫使我编写一个js包装器对象来获取我想要在DOM中的层次结构。
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()
-------------------------------------------------------
这是一个非常简单的例子...我想要对象参数等...
有没有人知道用扩展WebFrame窗口对象的对象传递子对象的方法?
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>
我是C ++编程的新手(来自web和python背景)。
I'm reasonably new to C++ programming (coming from a web and python background).
希望这个例子不仅可以帮助其他新用户,而且可以让更有经验的c ++程序员详细介绍。
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. :)
推荐答案
我有同样的问题,在这里找到答案:
http://doc.qt.nokia.com/4.7/qtwebkit-bridge.html
I had the same issue, and found an answer here: http://doc.qt.nokia.com/4.7/qtwebkit-bridge.html
访问子QObject
QObject的每个已命名子项
(即, QObject :: objectName()
不是空字符串)默认情况下可用作JavaScript包装器对象的属性。例如,如果您的QDialog具有其objectName属性为okButton的子窗口小部件,则可以通过表达式在脚本代码中访问此对象
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
由于objectName本身是 Q_PROPERTY
,您可以在脚本代码中操作名称,例如,重命名对象:
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
这篇关于Qt 4.6将对象和子对象添加到QWebView窗口对象(C ++和Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!