qwebview search all visible text

2019-08-16 12:50发布

how can I find a text from qwebview? I convert the document to plain text and use QString::comapare to find.

ui->webView->page()->mainFrame()->toPlainText();

but the method is unable to find text inside something like iframe.

how can I search all visible text?

标签: c++ string qt web
1条回答
smile是对你的礼貌
2楼-- · 2019-08-16 13:16

Qt represents a web page as an hierarchy structure of frames. So you need to use a recursive search.

bool findText(QWebFrame* frame, QString text)
{ 
  if (frame->toPlainText().contains(text))
  {
    return true;
  }
  foreach (QWebFrame* child, frame->childFrames())
  {
    if (findText(child, text)) return true;     
  }
  return false;
}

Use:

bool result = findText(ui->webView->page()->mainFrame(), QLatin1String("Text to find"));
查看更多
登录 后发表回答