PDA

View Full Version : Two webviews and webkit bridge. Only one webview works.



Wondering
17th June 2014, 20:49
Hi all,

I have a strange problem, that I can reproduce but don't understand. Yet.

I have a dialog, with two webviews (webview1, and webview2) and two spinBoxes (firstSpinBox and secondSpinBox)
I'd like webview1 to act (with javascript) on firstSpinBox and webview2 on secondSpinBox.

Here is some code to illustrate it:


MyDlg::MyDlg(QWidget *parent)
:QDialog(parent)
{
setupUi(this);

QWebFrame *first_frame = webview1->page()->mainFrame();
QWebFrame *second_frame = webview2->page()->mainFrame();

connect(first_frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(addJavaScriptObject()));
connect(second_frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(addJavaScriptObject()));

webview1->setUrl(QUrl("qrc:///assets/html/first.html"));
webview2->setUrl(QUrl("qrc:///assets/html/second.html"));
}

void MyDlg::addJavaScriptObject()
{
QWebFrame *first_frame = webview1->page()->mainFrame();
QWebFrame *second_frame = webview2->page()->mainFrame();

first_frame->addToJavaScriptWindowObject("first", firstSpinBox);
second_frame->addToJavaScriptWindowObject("second", secondSpinBox);
}


Now what happens is this:

Only webview1 (that is first_frame) recognizes firstSpinBox in its javascript.
webview2 (second_frame) doesn't recognize secondSpinBox.

The strange thing, or maybe the explanation, is if I load webview2 first, that is, if I do:



// Invert loading order
webview2->setUrl(QUrl("qrc:///assets/html/second.html"));
webview1->setUrl(QUrl("qrc:///assets/html/first.html"));


In that case, webview2 javascript recognizes secondSpinBox, but, webview1 doesn't recognize firstSpinBox.

I thought the two webviews were separate and don't share anything, but it looks like the event "javaScriptWindowObjectCleared())" acts on the whole app?

Thanks a lot in advance :)