Qt: How can I receive events fired by QWebElement objects?
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 li开发者_开发问答steners 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.
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.
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);
精彩评论