I am trying to highlight the selected text. Finding issue, when the same word(s) found before that line when FINDTEXT() is used
Since trigger action is set as START_OF_DOCUMENT
Kindly suggest an alternative to highlight text on the webview.
The code for the highlight text using find text is as below
{
page()->triggerAction(QWebPage::MoveToStartOfDocument, true);
page()->setContentEditable(true);
//qDebug() << txt;
this->findText(txt);
QVariant tmpVar
= this
->page
()->inputMethodQuery
(Qt
::ImMicroFocus);
qDebug() << tmpVar;
QRect tmpRect
= tmpVar.
toRect();
pt.setX(tmpRect.x());
pt.setY(tmpRect.y());
QWebHitTestResult hitRes = page()->mainFrame()->hitTestContent(pt);
QWebElement htmlElement = hitRes.enclosingBlockElement();
QString id
= htmlElement.
attribute("id");
if(id == paraid)
{
m_Color.setNamedColor("yellow");
QVariant va
= page
()->mainFrame
()->evaluateJavaScript
(js
);
page()->triggerAction(QWebPage::MoveToNextChar, true);
QVariant tmpVar
= page
()->inputMethodQuery
(Qt
::ImMicroFocus);
QRect tmpRect
= tmpVar.
toRect();
pt.setX(tmpRect.x());
pt.setY(tmpRect.y());
textpos = pt;
return;
}
else
{
while(page()->MoveToEndOfDocument)
{
page()->triggerAction(QWebPage::MoveToNextChar, true);
findtextpos(txt,paraid);
}
}
page()->setContentEditable(false);
}
{
hglist.clear();
// hglist.append(ldbook->getBookTitle());
findText(str);
m_Color.setNamedColor(color);
QVariant va
= page
()->mainFrame
()->evaluateJavaScript
(js
);
QVariant tmpVar
= page
()->inputMethodQuery
(Qt
::ImMicroFocus);
QRect tmpRect
= tmpVar.
toRect();
pt.setX(tmpRect.x());
pt.setY(tmpRect.y());
page()->inputMethodQuery(Qt::ImMicroFocus).toRect().center();
QWebHitTestResult hitRes = page()->mainFrame()->hitTestContent(pt);
QWebElement htmlElement = hitRes.enclosingBlockElement();
QString id
= htmlElement.
attribute("id");
if(hglist.isEmpty())
hglist<<str;
hglist.append(htmlElement.attribute("id"));
hglist.append(color);
hglist.append("");
hglist.append("");
hglist.append(htmlElement.toOuterXml());
storedbhighlights();
page()->triggerAction(QWebPage::MoveToNextChar);
}
void webview::findtextpos(QString txt, QString paraid)
{
page()->triggerAction(QWebPage::MoveToStartOfDocument, true);
page()->setContentEditable(true);
//qDebug() << txt;
this->findText(txt);
QVariant tmpVar = this->page()->inputMethodQuery(Qt::ImMicroFocus);
qDebug() << tmpVar;
QRect tmpRect = tmpVar.toRect();
QPoint pt;
pt.setX(tmpRect.x());
pt.setY(tmpRect.y());
QWebHitTestResult hitRes = page()->mainFrame()->hitTestContent(pt);
QWebElement htmlElement = hitRes.enclosingBlockElement();
QString id = htmlElement.attribute("id");
if(id == paraid)
{
QColor m_Color;
m_Color.setNamedColor("yellow");
QString clrName = m_Color.name();
QLatin1String cmd = QLatin1String("HiliteColor");
QString js = QString(QLatin1String("document.execCommand(\"%1\",null, \"%2\")")).arg(cmd).arg(clrName);
QVariant va = page()->mainFrame()->evaluateJavaScript(js);
page()->triggerAction(QWebPage::MoveToNextChar, true);
QVariant tmpVar = page()->inputMethodQuery(Qt::ImMicroFocus);
QRect tmpRect = tmpVar.toRect();
QPoint pt;
pt.setX(tmpRect.x());
pt.setY(tmpRect.y());
textpos = pt;
return;
}
else
{
while(page()->MoveToEndOfDocument)
{
page()->triggerAction(QWebPage::MoveToNextChar, true);
findtextpos(txt,paraid);
}
}
page()->setContentEditable(false);
}
void webview::applyhighlight(QString str, QString color)
{
hglist.clear();
// hglist.append(ldbook->getBookTitle());
findText(str);
QColor m_Color;
m_Color.setNamedColor(color);
QString clrName = m_Color.name();
QLatin1String cmd = QLatin1String("HiliteColor");
QString js = QString(QLatin1String("document.execCommand(\"%1\",null, \"%2\")")).arg(cmd).arg(clrName);
QVariant va = page()->mainFrame()->evaluateJavaScript(js);
QVariant tmpVar = page()->inputMethodQuery(Qt::ImMicroFocus);
QRect tmpRect = tmpVar.toRect();
QPoint pt;
pt.setX(tmpRect.x());
pt.setY(tmpRect.y());
page()->inputMethodQuery(Qt::ImMicroFocus).toRect().center();
QWebHitTestResult hitRes = page()->mainFrame()->hitTestContent(pt);
QWebElement htmlElement = hitRes.enclosingBlockElement();
QString id = htmlElement.attribute("id");
QStringList pno = id.split("\_");
if(hglist.isEmpty())
hglist<<str;
hglist.append(htmlElement.attribute("id"));
hglist.append(color);
hglist.append("");
hglist.append("");
hglist.append(htmlElement.toOuterXml());
storedbhighlights();
page()->triggerAction(QWebPage::MoveToNextChar);
}
To copy to clipboard, switch view to plain text mode
Added after 1 36 minutes:
Issue cropping up here is that say for example the word "THE" is highlighted in the 5 the line. But this word is also found in all the other previous 4 lines and multiple times as well. Now I am not able to identify the indented text to highlight.
I don't have para id as well.
How do I do this?
<Just an idea> Is it possible to get the index of the word from the start of the page. So that I can't again find this index and restore the highlight.
Bookmarks