I've done a QWebKit project in which I stablished a bridge between Javascript and my C++ code.
To achieve this I used the method:
this->page()->mainFrame()->addToJavaScriptWindowObject( "god", this );
This allows you to execute methods of the object you pass to addToJavaScriptWindowObject as second parameter from Javascript, using the object specified as first parameter.
Here an example:
class Browser : public QWebView
{
Q_OBJECT
public:
Browser( QWidget* parent=0 )
{
connect( this->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(onJavaScriptWindowObjectCleared()) );
}
public slots:
void onJavaScriptWindowObjectCleared()
{
// QString script = "console.log('init!');";
// this->page()->mainFrame()->evaluateJavaScript( script );
}
void onChange()
{
qDebug() << "Browser::onChange()";
}
}
Then, from Javascript I can do:
$('input:text').keydown( god.onChange );
So every time I press a key in an input box, god.onChange() is executed which executes Browser::onChange() slot.
This way you avoid extending the JS api.