Tools

Namespaces

Variants

Views

Actions

Search

Contents

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Exposing QObjects to Qt Webkit

Comments

Csinnott - Missing signal definition.

Something that wasn't clear to a newbie like me is that the signal code is generated.

csinnott
18:08, 10 April 2013 (EEST)

Hamishwillee - Can you fix?

Hi Csinnott

This article is 4 years old and the author is probably not monitoring it - and in addition Qt is no longer as important to Nokia as it once was. As this is a wiki you are encouraged to make changes to articles if you think they would benefit - do you think you could make the improvement you see?

Note also that this was validated last against an N97 (Qt 4.7, S60 5th Edition). If you're validating it against Symbian Belle or a more recent device could you please also update the ArticleMetaData to let people know?

mainwindow.cpp

#include "mainwindow.h"#include "ui_mainwindow.h"

#include <QtWebKit/QWebPage>#include <QtWebKit/QWebFrame>

MainWindow::MainWindow(QWidget*parent):QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this);/** * This is the interesting part, here we create the QObject * and connect the QWebViews pages main frames * javaScriptWindowObjectCleared signal to our addJavaScriptObject slot * which then exposes the QObject to the WebKit. * * QWebFrame::javaScriptWindowObjectCleared signal is emitted whenever * the global window object of the JavaScript environment is cleared, * e.g., before starting a new load. * If you intend to add QObjects to a QWebFrame using * addToJavaScriptWindowObject(), you should add them in a slot connected * to this signal. This ensures that your objects remain accessible when * loading new URLs. * http://doc.qt.nokia.com/4.7/qwebframe.html#javaScriptWindowObjectCleared */ m_sampleQObject =new SampleQObject(this);connect(ui->webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),this, SLOT(addJavaScriptObject()));}

voidMainWindow::addJavaScriptObject(){/** * This code calls another interesting method * QWebFrame::addJavaScriptObject * which can expose any QObject to the JavaScript context with the * name given as the first parameter. */ this->ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("sampleQObject", this->m_sampleQObject);}