我正在使用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#include #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#include #include #include #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 #includeclass HappyApi : public QObject { Q_OBJECT public: HappyApi(QObject *parent); public slots: void os_foo(); signals: void win_bar(); }; #endif // HAPPYAPI_H
happyapi.cpp
#include#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
Stackoverflow Question
我是C++编程的新手(来自web和python背景).
希望这个例子不仅可以帮助其他新用户,而且可以让更有经验的c ++程序员详细介绍.
感谢您提供的任何帮助.:)