views:

105

answers:

2

I want to receive notifications whenever links are clicked or text fields are changed in a Webkit instance in Qt. How can I hook up event listeners to a QWebElement?

I know I can put some Javascript on each element’s onchange / onclick handler, but I’m looking for a cleaner solution.

A: 

Looks like the only way (as of Qt 4.6) is JavaScript.

According to this TODO list for WebKit, there are plans to add a QWebElement::connectEvent() method.

Jen
+1  A: 

Using this function:

void QWebView::linkClicked ( const QUrl & url )   [signal]

This signal is emitted whenever the user clicks on a link and the page's linkDelegationPolicy property is set to delegate the link handling for the specified url.

You can access the HTML code as follows:

QString Widget::evalJS(const QString &js)
 {
     QWebFrame *frame = ui->webView->page()->mainFrame();
     return frame->evaluateJavaScript(js).toString();
 }

 evalJS(QString("document.forms[\"f\"].text.value = \"%1\";").arg(fromText));

 evalJS(QString("document.forms[\"f\"].langSelect.value = \"%1\";").arg(langText));

 evalJS(QString("translate()"));

 QString from = evalJS("document.forms[\"f\"].text.value");
 QString translation = evalJS("document.forms[\"f\"].translation.value");
 ui->textEditTo->setText(translation);
jordenysp
Thanks, but how about the "onchange" event?
Jen