gpt4 book ai didi

c++ - 如何在 QScintilla 编辑器小部件中取消选择/取消突出显示选定和突出显示的文本?

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:48 25 4
gpt4 key购买 nike

我正在尝试编写一个搜索框,其中包含查找/查找上一个/查找下一个以与 QScintilla textEditor 小部件进行交互。因此,我写了一些方法,一个用于突出显示所有匹配的单词然后选择第一次出现,一个用于取消选择/取消突出显示它们。

首先运行 seams 以完成它应该做的,但如果我用另一个关键字重复搜索,旧关键字和新关键字都会显示。

enter image description here

这是我的代码片段:

//
// search & replace:
// do_search_and_replace() - search for matching word
//
void MainWindow::do_search_and_replace(QString action_str)
{
int line, index;
qDebug() << "do_search_and_replace()";
// just to be sure...
if(action_str.isEmpty())
action_str == "0";

int action_nr = action_str.toInt(); // convert argument to int, so we can switch() on it...
text = ui->lineEdit_find->text();
docText = ui->textEdit->text();
qDebug() << "action_nr: " << action_nr;

//
// first part: Mark all occurances of search term
//
if (!( text.isEmpty() ))
{
qDebug() << text;
ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETSTYLE, 0, QsciScintilla::INDIC_FULLBOX);
ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETFORE,0, QColor(Qt::darkBlue));


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

if(end != -1)
{
ui->textEdit->getCursorPosition(&line, &index);
qDebug() << "line: " << line << " index: " << index;
while(cur != end)
{
cur = docText.indexOf(text,cur+1);
ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICATORFILLRANGE,cur,
text.length());
}
}
} // END text.isEmpty(), END mark ALL

//
// second part: Find firs occurance of search term
//
bool use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward;
use_regular_expression = false;
is_case_sensitive = ui->checkBox_CaseSensitive->isChecked();
match_whole_word_only = ui->checkBox_WholeWords->isChecked();
use_wrap = true;
search_forward = ui->radioButton_Forward->isChecked();

ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETSTYLE, 0, QsciScintilla::INDIC_FULLBOX);
//ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETFORE,0, QColor(Qt::darkBlue));

bool found = ui->textEdit->findFirst(text, use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward);
qDebug() << "START: found = " << found;
while(found)
{
ui->textEdit->getCursorPosition(&line, &index);

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

// pattern: found = findFirst(pattern, use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward)
//found = ui->textEdit->findFirst(text, use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward);

if(found)
{
ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICATORFILLRANGE, line, text.length());
int start = ui->textEdit->positionFromLineIndex(line, index);
int end = ui->textEdit->positionFromLineIndex(line, index + text.length());
qDebug() << "line: " << line << " start: " << start << " end: " << end;

// found = ui->textEdit->findNext();
// ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICATORFILLRANGE, line, text.length());

}

found = false;
}
}


//
// unselect selected stuff
//
void MainWindow::reset_searchResult()
{
int line, index;
qDebug() << "in: reset_searchResult()";

//QString text = ui->lineEdit_find->text();
text.clear();
docText.clear();

//
// first part: Mark all occurances of search term
//
if (( text.isEmpty() ))
{
qDebug() << text;
ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETSTYLE, 0, QsciScintilla::INDIC_PLAIN);
ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETFORE,0, QColor(Qt::white));

//docText = text;
docText = ui->lineEdit_find->text();
int end = docText.lastIndexOf(text);
int cur = -1;

if(end != -1)
{
ui->textEdit->getCursorPosition(&line, &index);
qDebug() << "line: " << line << " index: " << index;
while(cur != end)
{
cur = docText.indexOf(text,cur+1);
ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICATORALLONFOR,cur, text.length());
ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICATORCLEARRANGE,cur, text.length());
}
}
} // END text.isEmpty(), END mark ALL

//
// second part: Find firs occurance of search term
//
bool use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward;
use_regular_expression = false;
is_case_sensitive = ui->checkBox_CaseSensitive->isChecked();
match_whole_word_only = ui->checkBox_WholeWords->isChecked();
use_wrap = true;
search_forward = ui->radioButton_Forward->isChecked();

ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETSTYLE, 0, QsciScintilla::INDIC_PLAIN);
//ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICSETFORE,0, QColor(Qt::darkBlue));

bool found = ui->textEdit->findFirst(text, use_regular_expression, is_case_sensitive, match_whole_word_only, use_wrap, search_forward);
qDebug() << "START: found = " << found;
while(found)
{
ui->textEdit->getCursorPosition(&line, &index);

if(found)
{
ui->textEdit->SendScintilla(QsciScintillaBase::SCI_INDICATORCLEARRANGE, line, text.length());
int start = ui->textEdit->positionFromLineIndex(line, index);
int end = ui->textEdit->positionFromLineIndex(line, index + text.length());
qDebug() << "line: " << line << " start: " << start << " end: " << end;

}

found = false;
}
}

我很确定这不是有史以来最好的代码,但我能负担得起...;)

也许适合 QScintilla 的人可以纠正我的尝试或为我提供一个有效的搜索和突出显示示例,可以在不保留旧结果的情况下调用多次?

提前致谢,各位!

最佳答案

也许它比你想象的要容易,假设你为你的 ui->textEdit 使用了 QsciScintilla 实例:

#define MY_MARKER_ID 0

void MainWindow::clearMarkers()
{
int lastLine = ui->textEdit->lines() - 1;
ui->textEdit->clearIndicatorRange( 0, 0, lastLine, ui->textEdit->text( lastLine ).length() - 1, MY_MARKER_ID );
}

关于c++ - 如何在 QScintilla 编辑器小部件中取消选择/取消突出显示选定和突出显示的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54305745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com