QScintilla突出显示匹配单词

时间:2014-05-24 18:31:07

标签: qt qscintilla

我试图突出显示编辑器上的所有匹配单词,似乎无法弄清楚如何正确突出显示文本。我可以成功遍历所有找到的匹配但似乎无法找到正确的调用来突出显示它。这是我的代码:

bool found = true;  

while(found)
{
    editor->getCursorPosition(&line, &index);

    qDebug() << "line: " << line << " index: " << index;

    found = editor->findFirst(pattern, use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward);

    if(found)
    {
        int start = editor->positionFromLineIndex(line, index);
        int end = editor->positionFromLineIndex(line, index + pattern.length());

        qDebug() << "line: " << line << " start: " << start << " end: " << end;

        // Attempts to highlight
        editor->SendScintilla(QsciScintilla::SCI_INDICGETSTYLE, QsciScintilla::INDIC_BOX);
        editor->SendScintilla(QsciScintilla::SCI_INDICSETFORE, 0x007f00);
        //child[0]->SendScintilla(QsciScintilla::SCI_INDICATORFILLRANGE, start, end - start);
        editor->SendScintilla(QsciScintilla::SCI_INDICATORFILLRANGE, start, end - start);
        editor->setIndicatorForegroundColor(QColor(159, 144, 0));
      //  editor->setColor(QColor(159, 144, 0));**
    }
}

我的qDebug()显示它经过每一行并找到匹配和单词出现的位置。但是评论 //尝试突出显示下的代码是我似乎无法弄清楚的。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

You can try it.

SendScintilla(QsciScintillaBase::SCI_INDICSETSTYLE,0, INDIC_BOX);

QString docText = text();
int end = docText.lastIndexOf(findText);
int cur = -1; 

if(end != -1) {

   while(cur != end) {
        cur = docText.indexOf(findText,cur+1);`
        SendScintilla(QsciScintillaBase::SCI_INDICATORFILLRANGE,cur,
            findText.length());
   }

}