QTextCursor does not move
Hi, I am using QTextCursor to move my cursor at the Start and at the End of my QTextEdit document.
Strangely, while I can move it to the Start with QTextCursor::Start, it seems it does not move to the end with QTextCursor::End
Here is my code
Code:
{
if ( !uiL->textEdit->find(text, flags) ) {
// if searching backward move cursor at the end
uiL->textEdit->setTextCursor(mycursor);
mycursor.
movePosition(QTextCursor::End);
// THIS DOES NOT WORK, OR AT LEAST IT SEEMS SO }
// if searching foreward move cursor at the start
uiL->textEdit->setTextCursor(mycursor);
mycursor.
movePosition(QTextCursor::Start);
// THIS WORKS }
// once cursor is moved search again. If return false than the word does not exist in the doc
if ( !uiL->textEdit->find(text, flags) ) {
qDebug()<<"Not found";
}
}
}
So basically when I am searching backward I always get "Not Found" when reached the top, I would expect to find the first occurrence from the bottom instead. DOes it make sense?
Thanks,
Jan
Re: QTextCursor does not move
Quote:
Originally Posted by
dotjan
Hi, I am using
QTextCursor to move my cursor at the Start and at the End of my
QTextEdit document.
Strangely, while I can move it to the Start with QTextCursor::Start, it seems it does not move to the end with QTextCursor::End
Here is my code
Code:
{
if ( !uiL->textEdit->find(text, flags) ) {
// if searching backward move cursor at the end
mycursor.
movePosition(QTextCursor::End);
// try here instead uiL->textEdit->setTextCursor(mycursor);
}
// if searching foreward move cursor at the start
uiL->textEdit->setTextCursor(mycursor);
mycursor.
movePosition(QTextCursor::Start);
// THIS WORKS - actually, this doesnt do anything... }
// once cursor is moved search again. If return false than the word does not exist in the doc
if ( !uiL->textEdit->find(text, flags) ) {
qDebug()<<"Not found";
}
}
}
So basically when I am searching backward I always get "Not Found" when reached the top, I would expect to find the first occurrence from the bottom instead. DOes it make sense?
Thanks,
Jan
see code changes
do you know the reason why your way cannot work?
Re: QTextCursor does not move
Hi, actually it does not work either with your changes. But I have now found the solution by a chance..
Code:
if ( !uiL->textEdit->find(text, flags) ) {
// if searching foreward, set backward flag and move cursor at the start
uiL->textEdit->setTextCursor(mycursor);
}
uiL->textEdit->setTextCursor(mycursor);
}
}
Re: QTextCursor does not move
you move cursor to end when searching backwards - it doesn't match your comment.
did you understand why your initial way will never work - it is an important concept/mechanism of c++!