QPlainTextEdit - 将文档搜索到末尾,然后从头开始再次搜索

问题描述 投票:0回答:1

我想在 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);
:

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

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


基于这个答案,我为

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();
        }
    }
}
c++ qt qplaintextedit qtextcursor
1个回答
2
投票

这个想法是在开始搜索单词之前保持光标位置。然后,在研究之后,您将把光标设置回您保存的位置 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); } } }

© www.soinside.com 2019 - 2024. All rights reserved.