QPlainTextEdit - 从头开始​​搜索文档到最后

时间:2016-06-02 17:46:15

标签: 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();
    }
}
}

1 个答案:

答案 0 :(得分:1)

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

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);
        }
    }
}
相关问题