Results 1 to 3 of 3

Thread: Browsing search text upwards and downwards

  1. #1
    Join Date
    Oct 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Browsing search text upwards and downwards

    Hi
    I have written following code to find occurrences of a given text in whole text document. Following code will highlight all such occurrences. But when I click search down / search up buttons it doesn't move cursor to next occurrence. Can anyone please help to achieve this task.
    bool bDown parameter is intended to be used to indicate whether to move cursor up/down. But Still I am clueless of how to use this parameter.

    Qt Code:
    Qt Code:
    1. QsciScintilla *m_pTextEdit;
    2.  
    3. void searchText( const QString& sText, bool bDown)
    4. {
    5. QString data = m_pTextEdit->text();
    6. m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICATORCLEARRANGE, 0, data.length());
    7.  
    8. if (sText.isEmpty())
    9. {
    10. return;
    11. }
    12.  
    13. int index = data.indexOf(sText, 0, Qt::CaseInsensitive);
    14.  
    15. while (index >= 0) {
    16. int length = sText.length();
    17. m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICSETSTYLE,0 ,QsciScintilla::INDIC_ROUNDBOX);
    18. m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICSETFORE, 0x007f00);
    19.  
    20. m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICATORFILLRANGE, index, length);
    21. index = data.indexOf(sText, index + length, Qt::CaseInsensitive);
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,552
    Thanks
    13
    Thanked 1,579 Times in 1,508 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Browsing search text upwards and downwards

    Probably a combination of SCi_SETCURRENTPOS(), SCI_SETANCHOR() and SCI_SCROLLCARET().

    I would probably have used the Scintilla searching functions to locate the text.
    http://www.scintilla.org/ScintillaDoc.html#Searching

  3. #3
    Join Date
    Oct 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Browsing search text upwards and downwards

    Thanks As suggested by you,
    I modified code block as follows.
    Qt Code:
    1. void searchText( const QString& sText, bool bDown)
    2. {
    3. QString data = m_pTextEdit->text();
    4. m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICATORCLEARRANGE, 0, data.length());
    5.  
    6. if (sText.isEmpty())
    7. {
    8. return;
    9. }
    10.  
    11. int index = data.indexOf(sText, 0, Qt::CaseInsensitive);
    12. int length = sText.length();
    13.  
    14. if (bDown == true)
    15. {
    16. m_pTextEdit->SendScintilla(QsciScintilla::SCI_SETANCHOR, index);
    17. m_pTextEdit->SendScintilla(QsciScintilla::SCI_SETCURRENTPOS, index);
    18. m_pTextEdit->SendScintilla(QsciScintilla::SCI_SCROLLCARET, 0);
    19. edit_SearchText->setFocus();
    20. }
    21.  
    22. while (index >= 0) {
    23. m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICSETSTYLE,0 ,QsciScintilla::INDIC_ROUNDBOX);
    24. m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICSETFORE, 0x007f00);
    25.  
    26. m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICATORFILLRANGE, index, length);
    27. index = data.indexOf(sText, index + length, Qt::CaseInsensitive);
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    Still no luck of moving selection to next occurrence when search down key is pressed.

Similar Threads

  1. Highlight text given a search string
    By jrsanders in forum Qt Programming
    Replies: 0
    Last Post: 29th May 2015, 17:56
  2. How do I search for files which contain a specific text string?
    By rolandburt in forum General Programming
    Replies: 1
    Last Post: 20th August 2012, 21:32
  3. Thread Search in text file large
    By marcos.miranda in forum Newbie
    Replies: 11
    Last Post: 12th June 2012, 20:54
  4. Replies: 4
    Last Post: 25th May 2008, 20:01
  5. Text search utility
    By NewGuy in forum Newbie
    Replies: 7
    Last Post: 23rd July 2006, 11:59

Tags for this Thread

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.