c++ - QPlainTextEdit - 搜索文档到最后,然后再从头开始

标签 c++ qt find qplaintextedit qtextcursor

我想在 QPlainTextEdit 中搜索从当前光标到末尾的字符串。如果什么都找不到,我想从头开始继续搜索。只有在这个阶段,如果什么都没有找到,就会出现一条消息。 这是代码:

void BasicEdit::findString(QString s, bool reverse, bool casesens, bool words) {
QTextDocument::FindFlags flag;
if (reverse) flag |= QTextDocument::FindBackward;
if (casesens) flag |= QTextDocument::FindCaseSensitively;
if (words) flag |= QTextDocument::FindWholeWords;

QTextCursor cursor = this->textCursor();

if (!find(s, flag)) {
    //nothing is found | jump to start/end
    cursor.movePosition(reverse?QTextCursor::End:QTextCursor::Start);
    setTextCursor(cursor); //!!!!!!
    if (!find(s, flag)) {
        //no match in whole document
        QMessageBox msgBox;
        msgBox.setText(tr("String not found."));
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        msgBox.exec();
    }
  }
}

问题是 setTextCursor(cursor);

  • 没有这一行,搜索不会从头/尾继续
  • 对于这一行,一切都很好,除了当找不到字符串时,光标位于文档的开头/结尾,并且用户在文档中的当前位置丢失。

如何在文档中搜索一个字符串,如果没有找到则不改变当前位置?


更新

感谢 IAmInPLS,代码如下所示。 我为 verticalScrollBar 添加了保值。 即便如此,当没有发现生成的内容时,会出现短暂的闪烁:cursor.movePosition(reverse?QTextCursor::End:QTextCursor::Start);

我们怎样才能摆脱它? 怎样才能看起来像专业的编辑? 创建另一个不可见的 QPlainTextEdit 元素以在其中进行搜索是一种想法吗?

void BasicEdit::findString(QString s, bool reverse, bool casesens, bool words)
{
QTextDocument::FindFlags flag;
if (reverse) flag |= QTextDocument::FindBackward;
if (casesens) flag |= QTextDocument::FindCaseSensitively;
if (words) flag |= QTextDocument::FindWholeWords;

QTextCursor cursor = this->textCursor();
// here we save the cursor position and the verticalScrollBar value
QTextCursor cursorSaved = cursor;
int scroll = verticalScrollBar()->value();

if (!find(s, flag))
{
    //nothing is found | jump to start/end
    cursor.movePosition(reverse?QTextCursor::End:QTextCursor::Start);
    setTextCursor(cursor);

    if (!find(s, flag))
    {
        // word not found : we set the cursor back to its initial position and restore verticalScrollBar value
        setTextCursor(cursorSaved);
        verticalScrollBar()->setValue(scroll);
        QMessageBox msgBox(this);
        msgBox.setText(tr("String not found."));
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        msgBox.exec();
    }
}
}

最佳答案

这个想法是在开始搜索单词之前保持光标位置。然后,在研究之后,您会将光标设置回您保存的位置

void BasicEdit::findString(QString s, bool reverse, bool casesens, bool words) 
{
    QTextDocument::FindFlags flag;
    if (reverse) flag |= QTextDocument::FindBackward;
    if (casesens) flag |= QTextDocument::FindCaseSensitively;
    if (words) flag |= QTextDocument::FindWholeWords;

    QTextCursor cursor = this->textCursor();
    // here , you save the cursor position
    QTextCursor cursorSaved = cursor;

    if (!find(s, flag)) 
    {
        //nothing is found | jump to start/end
        cursor.movePosition(reverse?QTextCursor::End:QTextCursor::Start);

        /* following line :
        - the cursor is set at the beginning/end of the document (if search is reverse or not)
        - in the next "find", if the word is found, now you will change the cursor position
        */ 
        setTextCursor(cursor);

        if (!find(s, flag)) 
        {
            //no match in whole document
            QMessageBox msgBox;
            msgBox.setText(tr("String not found."));
            msgBox.setStandardButtons(QMessageBox::Ok);
            msgBox.setDefaultButton(QMessageBox::Ok);
            msgBox.exec();

            // word not found : we set the cursor back to its initial position
            setTextCursor(cursorSaved);
        }
    }
}

关于c++ - QPlainTextEdit - 搜索文档到最后,然后再从头开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37598807/

相关文章:

c++ - 针对 std::function 的 lambda 表达式和模板推导:为什么这有效?

c++ - 如何在 QVBoxLayout 中显示 QString

php - 如何使用 PHP 查找序列中缺失的值?

algorithm - 检测 "bitmap"中的形状

c++ - Windows 8 将流传递给 IMFSourceReader

c++ - 使用 g++ 将多个源文件编译成一个 .o

c++ - 如何让 Box2D 只创建一个实体实例?

qt - QML MenuItem 突出显示不起作用

c++ - QNetworkAccessManager,重置 TCP 连接

regex - Linux - 在路径中的每个斜杠后查找与正则表达式匹配的结果