Results 1 to 13 of 13

Thread: Highlight text issue

  1. #1
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Highlight text issue

    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
    Qt Code:
    1. void webview::findtextpos(QString txt, QString paraid)
    2. {
    3. page()->triggerAction(QWebPage::MoveToStartOfDocument, true);
    4. page()->setContentEditable(true);
    5. //qDebug() << txt;
    6. this->findText(txt);
    7.  
    8. QVariant tmpVar = this->page()->inputMethodQuery(Qt::ImMicroFocus);
    9. qDebug() << tmpVar;
    10. QRect tmpRect = tmpVar.toRect();
    11. QPoint pt;
    12. pt.setX(tmpRect.x());
    13. pt.setY(tmpRect.y());
    14. QWebHitTestResult hitRes = page()->mainFrame()->hitTestContent(pt);
    15. QWebElement htmlElement = hitRes.enclosingBlockElement();
    16. QString id = htmlElement.attribute("id");
    17. if(id == paraid)
    18. {
    19. QColor m_Color;
    20. m_Color.setNamedColor("yellow");
    21. QString clrName = m_Color.name();
    22. QLatin1String cmd = QLatin1String("HiliteColor");
    23. QString js = QString(QLatin1String("document.execCommand(\"%1\",null, \"%2\")")).arg(cmd).arg(clrName);
    24. QVariant va = page()->mainFrame()->evaluateJavaScript(js);
    25. page()->triggerAction(QWebPage::MoveToNextChar, true);
    26. QVariant tmpVar = page()->inputMethodQuery(Qt::ImMicroFocus);
    27. QRect tmpRect = tmpVar.toRect();
    28. QPoint pt;
    29. pt.setX(tmpRect.x());
    30. pt.setY(tmpRect.y());
    31. textpos = pt;
    32. return;
    33. }
    34.  
    35. else
    36. {
    37. while(page()->MoveToEndOfDocument)
    38. {
    39. page()->triggerAction(QWebPage::MoveToNextChar, true);
    40. findtextpos(txt,paraid);
    41. }
    42. }
    43.  
    44. page()->setContentEditable(false);
    45.  
    46. }
    47.  
    48. void webview::applyhighlight(QString str, QString color)
    49. {
    50.  
    51. hglist.clear();
    52. // hglist.append(ldbook->getBookTitle());
    53. findText(str);
    54. QColor m_Color;
    55. m_Color.setNamedColor(color);
    56. QString clrName = m_Color.name();
    57. QLatin1String cmd = QLatin1String("HiliteColor");
    58. QString js = QString(QLatin1String("document.execCommand(\"%1\",null, \"%2\")")).arg(cmd).arg(clrName);
    59. QVariant va = page()->mainFrame()->evaluateJavaScript(js);
    60. QVariant tmpVar = page()->inputMethodQuery(Qt::ImMicroFocus);
    61. QRect tmpRect = tmpVar.toRect();
    62. QPoint pt;
    63. pt.setX(tmpRect.x());
    64. pt.setY(tmpRect.y());
    65. page()->inputMethodQuery(Qt::ImMicroFocus).toRect().center();
    66. QWebHitTestResult hitRes = page()->mainFrame()->hitTestContent(pt);
    67. QWebElement htmlElement = hitRes.enclosingBlockElement();
    68. QString id = htmlElement.attribute("id");
    69. QStringList pno = id.split("\_");
    70. if(hglist.isEmpty())
    71. hglist<<str;
    72.  
    73. hglist.append(htmlElement.attribute("id"));
    74. hglist.append(color);
    75. hglist.append("");
    76. hglist.append("");
    77. hglist.append(htmlElement.toOuterXml());
    78. storedbhighlights();
    79. page()->triggerAction(QWebPage::MoveToNextChar);
    80.  
    81. }
    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.
    Last edited by ejoshva; 27th March 2015 at 12:17.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Highlight text issue

    Sorry, what is the question and how is the code snippet related to it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Highlight text issue

    I want to highlight the selected word on the page displayed in QGraphicsWebView.

    Currently I am doing the highlight using findText().
    But the issue I am facing is that if there is more than one occurrence of the particular word, only first occurrence is highlighted and not the intended.
    For example : I am highlighting "1940". In that particular 1940 is found 4 times. I intend to highlight the 3rd occurrence. But only the 1st occurrence is highlighted since before findtext(), page()->triggerAction(Qt::MoveToStartOfDocument).
    When I restore the highlight after navigating to next page and coming back also finding the same issue.

    I just got a plain idea as to loop through whole page and find all occurrence of selected word using findText() and match the current mouseReleasePoint and match against the location of each occurrence and highlight at the exact matching location.
    Not sure how to implement this. Since I am not able to find the location of each occurrence of the word.

    rough coding
    Qt Code:
    1. QPoint webview::findTextInPage(QString searchText, QPoint hitMatchPoint, QString color)
    2. {
    3. QPoint pt;
    4. page()->setContentEditable(true);
    5. page()->triggerAction(QWebPage::MoveToStartOfDocument);
    6.  
    7. while (findText(searchText))
    8. {
    9. QVariant tmpVar = page()->inputMethodQuery(Qt::ImMicroFocus);
    10. QRect tmpRect = tmpVar.toRect();
    11.  
    12. pt.setX(tmpRect.x());
    13. pt.setY(tmpRect.y());
    14.  
    15. if( pt == hitMatchPoint)
    16. {
    17. QColor m_Color;
    18. m_Color.setNamedColor(color);
    19. QString clrName = m_Color.name();
    20. QLatin1String cmd = QLatin1String("HiliteColor");
    21. QString js = QString(QLatin1String("document.execCommand(\"%1\",null, \"%2\")")).arg(cmd).arg(clrName);
    22. QVariant va = page()->mainFrame()->evaluateJavaScript(js);
    23. }
    24.  
    25. qDebug()<<"hitMatchPoint : "<<hitMatchPoint;
    26. qDebug()<<" Impact Point : "<<pt;
    27. }
    28. page()->setContentEditable(false);
    29. return pt;
    30. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ejoshva; 28th March 2015 at 03:02.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Highlight text issue

    Quote Originally Posted by ejoshva View Post
    I want to highlight the selected word on the page displayed in QGraphicsWebView.
    Why don't you use QGraphicsWebView::findText()? It has a flag to highlight all findings.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Highlight text issue

    I am using that only. But when it encounters any regular expression like /[^\n]* ?=()
    it's not getting highlighted.

    Eg : when this below is selected for highlight, findText doesn't work.
    372–5

    Eg : when below 3 lines are selected to be highlighted, highlight doesn't work

    As you work through this topic, look for information that will help you to answer these questions:
    How did societies change during the period between ancient and early modern times?
    How did beliefs and values influence societies in this period?


    Kindly let me how to include all the regular expression in the findtext()

  6. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Highlight text issue

    Is your regular expression a typo? Doesn't look like a valid regular expression to me and certainly won't match any of the 3 lines you listed.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Highlight text issue

    I don't think findText supports regular expressions.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Highlight text issue

    If findText() doesn't support, is there any other function which I can make use of?

    Quote Originally Posted by jefftee View Post
    Is your regular expression a typo? Doesn't look like a valid regular expression to me and certainly won't match any of the 3 lines you listed.
    What I meant was, if any of those in the regex appears in the selected text then the findText doesn't work

  9. #9
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Highlight text issue

    I was hung up on the fact that your regex didn't seem valid to me but as wysota pointed out, it doesn't matter because findText doesn't support QRegExp or QRegularExpression.

    I am not aware of an alternative unfortunately, sorry!

  10. #10
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Highlight text issue

    For highlight, found something as QSyntaxHighlighter. Checking on it now.
    http://doc.qt.io/qt-5/qtwidgets-rich...r-example.html
    Last edited by ejoshva; 31st March 2015 at 06:08.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Highlight text issue

    QSyntaxHighlighter will not work with a web view.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Highlight text issue

    . Then any other alternative

  13. #13
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Highlight text issue

    Still couldn't find any solution for this highlight word issue

Similar Threads

  1. find text with regexp in qwebview +highlight
    By solook in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2013, 14:36
  2. Highlight the text in QWidget on mouseMoveEvent
    By mythili in forum Qt Programming
    Replies: 6
    Last Post: 24th April 2013, 09:43
  3. Replies: 1
    Last Post: 5th August 2008, 21:44
  4. Setting a highlight text color for QTableWidgetItem
    By Hiba in forum Qt Programming
    Replies: 6
    Last Post: 14th December 2007, 11:51
  5. Highlight text in list box
    By Sheetal in forum Qt Tools
    Replies: 7
    Last Post: 9th April 2007, 12:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.