qt 操作html,如何在Webkit窗口中操作页面内容(使用QT和QTWebKit)?
請查看下面的示例.它使用
QWebView加載谷歌頁面.然后使用QWebFrame類查找與搜索編輯框和“谷歌搜索”按鈕對應的Web元素.使用搜索查詢更新編輯框,然后單擊搜索按鈕.
加載頁面:
QWebView::connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(on_pageLoad_finished(bool)));
QUrl url("http://www.google.com/");
ui->webView->load(url);
on_pageLoad_finished實現:
void MainWindow::on_pageLoad_finished(bool ok)
{
if (ok)
{
QWebFrame* frame = ui->webView->page()->currentFrame();
if (frame!=NULL)
{
// set google seatch box
QWebElementCollection collection = frame->findAllElements("input[name=q]");
foreach (QWebElement element, collection)
element.setAttribute("value", "how-to-manipulate-pages-content-inside-webkit-window-using-qt-and-qtwebkit");
// find search button
QWebElementCollection collection1 = frame->findAllElements("input[name=btnG]");
foreach (QWebElement element, collection1)
{
QPoint pos(element.geometry().center());
// send a mouse click event to the web page
QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(ui->webView->page(), &event0);
QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(ui->webView->page(), &event1);
}
}
}
}
希望這有幫助,問候
總結
以上是生活随笔為你收集整理的qt 操作html,如何在Webkit窗口中操作页面内容(使用QT和QTWebKit)?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html css integrity,H
- 下一篇: 数据与知识管理属于计算机科学研究方向中的