![](/img/trans.png)
[英]How to convert JavaScript array (or delimited string) to an object with sub-objects
[英]Qt 4.6 Adding objects and sub-objects to QWebView window object (C++ & Javascript)
我正在使用Qt的QWebView,並且已經發現了許多用於添加到webkit窗口對象的很多用途。
我想做的一件事是嵌套對象......例如:
在Javascript我可以...
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中的層次結構。
>>>api
-------------------------------------------------------
- api Object {os=function, more... }
- os_foo function()
- win_bar function()
-------------------------------------------------------
這是一個非常簡單的例子......我想要參數的對象等...
有沒有人知道使用擴展WebFrame窗口對象的對象傳遞子對象的方法?
這是我如何添加對象的一些示例代碼:
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背景)。
希望這個例子不僅可以幫助其他新用戶,而且可以讓更有經驗的c ++程序員詳細介紹。
感謝您提供的任何幫助。 :)
我遇到了同樣的問題,並在此處找到了答案: http : //doc.qt.nokia.com/4.7/qtwebkit-bridge.html
訪問子QObject
在每一個名為兒童QObject
(也就是說,對於其QObject::objectName()
不是一個空字符串)是可作為JavaScript包裝對象的屬性默認值。 例如,如果您的QDialog具有其objectName屬性為“okButton”的子窗口小部件,則可以通過表達式在腳本代碼中訪問此對象
myDialog.okButton
由於objectName本身是Q_PROPERTY
,因此您可以在腳本代碼中操作名稱,例如,重命名對象:
myDialog.okButton
myDialog.okButton.objectName = "cancelButton";
// from now on, myDialog.cancelButton references the button
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.