PDA

View Full Version : QTextCursor does not move



dotjan
15th November 2012, 18:10
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




void MyPage::find(const QString &text, QTextDocument::FindFlags flags)
{
if ( !uiL->textEdit->find(text, flags) ) {

// if searching backward move cursor at the end
if ( flags == QTextDocument::FindBackward ) {

QTextCursor mycursor;
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
if ( flags != QTextDocument::FindBackward ) {
QTextCursor mycursor;
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

amleto
15th November 2012, 21:31
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




void MyPage::find(const QString &text, QTextDocument::FindFlags flags)
{
if ( !uiL->textEdit->find(text, flags) ) {

// if searching backward move cursor at the end
if ( flags == QTextDocument::FindBackward ) {

QTextCursor mycursor;
mycursor.movePosition(QTextCursor::End); // try here instead
uiL->textEdit->setTextCursor(mycursor);
}

// if searching foreward move cursor at the start
if ( flags != QTextDocument::FindBackward ) {
QTextCursor mycursor;
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?

dotjan
16th November 2012, 01:42
Hi, actually it does not work either with your changes. But I have now found the solution by a chance..


if ( !uiL->textEdit->find(text, flags) ) {

// if searching foreward, set backward flag and move cursor at the start
if ( flags == QTextDocument::FindBackward ) {
QTextCursor mycursor = uiL->textEdit->textCursor();
mycursor.movePosition(QTextCursor::End);
uiL->textEdit->setTextCursor(mycursor);
}

if ( flags != QTextDocument::FindBackward ) {
QTextCursor mycursor = uiL->textEdit->textCursor();
mycursor.movePosition(QTextCursor::Start);
uiL->textEdit->setTextCursor(mycursor);
}
}

amleto
16th November 2012, 11:00
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++!