Problem in Moving QTextCursor to the End
Asked Answered
J

2

9

I'm trying to implement a simple text search in an editor i'm writing. Everything have been fine until this problem! I'm trying to implement a backward search here. The procedure is: look for the subject backward, if not found, beep once, and if find button was pressed again, go to the end of the document, and do the search again. "reachedEnd" is an int, defined as a private member of the editor class. Here's the function that does the backward search.

void TextEditor::findPrevPressed() {
    QTextDocument *document = curTextPage()->document();
    QTextCursor    cursor   = curTextPage()->textCursor();

    QString find=findInput->text(), replace=replaceInput->text();


    if (!cursor.isNull()) {
        curTextPage()->setTextCursor(cursor);
        reachedEnd = 0;
    }
    else {
        if(!reachedEnd) {
            QApplication::beep();
            reachedEnd = 1;
        }
        else {
            reachedEnd = 0;
            cursor.movePosition(QTextCursor::End);
            curTextPage()->setTextCursor(cursor);
            findPrevPressed();
        }
    }
}

The problem is that cursor doesn't move to the end! And it returns False, which means failure. How can this fail?!! Thanks in advance.

Jahdai answered 22/7, 2011 at 6:58 Comment(0)
D
10

Since this question got some views and it appears to be a common problem, I think it deserves an answer (even though the author most surely figured it out).

From the documentation:

QTextCursor QPlainTextEdit::textCursor() const
Returns a copy of the QTextCursor that represents the currently visible cursor. Note that changes on the returned cursor do not affect QPlainTextEdit's cursor; use setTextCursor() to update the visible cursor.

So you got a copy of it and by doing cursor.movePosition(QTextCursor::End); it wouldn't work.

What I did is:

QTextCursor newCursor = new QTextCursor(document);
newCursor.movePosition(QTextCursor::End);
curTextPage()->setTextCursor(newCursor);
Dynamotor answered 1/6, 2016 at 18:28 Comment(3)
How does one do this with a QTextEdit?Hypopituitarism
@Hypopituitarism so if your textbox is your qtextedit object, then you'd do QTextCursor newCursor = textbox->textCursor(); newCursor.movePosition(QTextCursor::End); textbox->setTextCursor(newCursor);Zabrine
Remove the newAssert
E
4

If I simplify your code like this:

if (!cursor.isNull()) {
   // (...)
}
else {
    // (...)
    cursor.movePosition(QTextCursor::End);
    // (...)
}

...I see that you call the movePosition() function while the cursor.isNull() condition is true. Maybe this is the reason it doesn't work...

Encrust answered 22/7, 2011 at 15:47 Comment(1)
I appreciate your help, but cursor.isNull() returns whether cursor is pointing to a position in the document. That doesn't mean that the cursor is a null pointer or something (AFAIK) and cursor is not a pointer here. But I think this was a useful hint, I will check the documentation again. Thanks indeed.Jahdai

© 2022 - 2024 — McMap. All rights reserved.