PDA

View Full Version : Browsing search text upwards and downwards



YatShan
2nd October 2015, 05:54
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:


QsciScintilla *m_pTextEdit;

void searchText( const QString& sText, bool bDown)
{
QString data = m_pTextEdit->text();
m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICATORCLEARRAN GE, 0, data.length());

if (sText.isEmpty())
{
return;
}

int index = data.indexOf(sText, 0, Qt::CaseInsensitive);

while (index >= 0) {
int length = sText.length();
m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICSETSTYLE,0 ,QsciScintilla::INDIC_ROUNDBOX);
m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICSETFORE, 0x007f00);

m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICATORFILLRANG E, index, length);
index = data.indexOf(sText, index + length, Qt::CaseInsensitive);
}
}



Thanks in advance :)

ChrisW67
4th October 2015, 22:12
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

YatShan
6th October 2015, 12:29
Thanks :) As suggested by you,
I modified code block as follows.


void searchText( const QString& sText, bool bDown)
{
QString data = m_pTextEdit->text();
m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICATORCLEARRAN GE, 0, data.length());

if (sText.isEmpty())
{
return;
}

int index = data.indexOf(sText, 0, Qt::CaseInsensitive);
int length = sText.length();

if (bDown == true)
{
m_pTextEdit->SendScintilla(QsciScintilla::SCI_SETANCHOR, index);
m_pTextEdit->SendScintilla(QsciScintilla::SCI_SETCURRENTPOS, index);
m_pTextEdit->SendScintilla(QsciScintilla::SCI_SCROLLCARET, 0);
edit_SearchText->setFocus();
}

while (index >= 0) {
m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICSETSTYLE,0 ,QsciScintilla::INDIC_ROUNDBOX);
m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICSETFORE, 0x007f00);

m_pTextEdit->SendScintilla(QsciScintilla::SCI_INDICATORFILLRANG E, index, length);
index = data.indexOf(sText, index + length, Qt::CaseInsensitive);
}
}


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