qwebview 搜索所有可见文本 [英] qwebview search all visible text

查看:20
本文介绍了qwebview 搜索所有可见文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 qwebview 中找到文本?我将文档转换为纯文本并使用 QString::comapare 查找.

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();

但是该方法无法在 iframe 之类的东西中找到文本.

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

如何搜索所有可见文本?

how can I search all visible text?

推荐答案

Qt 将网页表示为框架的层次结构.所以你需要使用递归搜索.

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;
}

使用:

bool result = findText(ui->webView->page()->mainFrame(), QLatin1String("Text to find"));

这篇关于qwebview 搜索所有可见文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆