Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Replace text from where user left the cursor



  • Hi,

    I developed a Find/Replace feature for my editor. In case of replaceText(), the text is being replaced from the start of the document. Ideally, it should start replacing from where the user left the cursor position. Please help me regarding this.

    My code:
    @
    void MyEditor::replaceText(const QString &findWord, const QString &replaceWord)
    {
    if(textEdit->find(findWord, QTextDocument::FindCaseSensitively))
    {
    textEdit->insertPlainText(replaceWord);
    }
    }
    @
    I need to start replacing text not from START of doc, but from where user had left the cursor.

    Thanks,
    Haney.

    [edit] code wrappings added, koahnig



  • Please use "code wrappings":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 when posting code sections



  • You could work with the find() methods of the [[Doc:QTextDocument]] attached to the QTextEdit. Those methods take a [[Doc:QTextCursor]] or int argument as the starting point of the search.


Log in to reply