qtextcursor setposition doesn't work
Hello,
in a program I have the following code:
Code:
void FindWidget::slotClose() {
cursor = mTextpage->textCursor();
qDebug() << "cursor position on close start: " << cursor.position();
if ( cursor.position() == -1) {
qDebug() << "cursor position after setPosition: " << cursor.position();
mTextpage->setTextCursor(cursor);
}
mTextpage->setFocus();
close();
}
Example output is the following
Code:
cursor position on close start: -1
cursor position after setPosition: -1
Anyone got an idea, what's goind wrong here? I also tried, not to use cursor as member and used the following instead:
Also, same result.
Thanks in advance for any help.
Re: qtextcursor setposition doesn't work
You're not changing the position of the cursor. For this:-
Code:
cursor.setPosition(int);
Re: qtextcursor setposition doesn't work
but in line 5, I have
I also tried the following line instead:
Code:
cursor.setPosition(0);
Re: qtextcursor setposition doesn't work
Oh! I didnt see that.
Code:
void FindWidget::slotClose() {
cursor = mTextpage->textCursor();
qDebug() << "cursor position on close start: " << cursor.position();
if ( cursor.position() == -1) {
qDebug() << "cursor position after setPosition: " << cursor.position();
mTextpage->setTextCursor(cursor);
}
mTextpage->setFocus();
close();
}
got it or not.
Dont forget to hit thanks button.
2 Attachment(s)
Re: qtextcursor setposition doesn't work
Thanks for your answer, but I this also doesn't work, same result with this.
The output again shows -1 for the position of the cursor after the moveposition.
I've got more and more the impression, that I'm dealing with a bug here.
I tried the following lines instead of line 5, nothing worked correctly:
Code:
cursor.setPosition(0);
In line 2, I use
I've added the .cpp and the .h file. Perhaps the I missed something else, what's wrong.
Re: qtextcursor setposition doesn't work
Could you state what is the result you are trying to achieve?
Re: qtextcursor setposition doesn't work
The cursor gets the position -1, when no match is found in the text. Unfortunately, the text isn't editable anymore after the closing of the findwidget, if the cursor has position -1.
Now my aim is, that I want to set the cursor to the start of the text, if no match is found and the widget is closed.
Re: qtextcursor setposition doesn't work
Quote:
Originally Posted by
doc_symbiosis
The cursor gets the position -1, when no match is found in the text. Unfortunately, the text isn't editable anymore after the closing of the findwidget, if the cursor has position -1.
So don't put it at position -1 when there is no match.
Quote:
Now my aim is, that I want to set the cursor to the start of the text, if no match is found and the widget is closed.
Code:
textEdit->setTextCursor(cursor);
Re: qtextcursor setposition doesn't work
But the cursor is set to -1 when no match is found by the find method in the findslot:
Code:
void FindWidget::slotFind()
{
if (cursor.anchor() == -1) {
cursor
= mTextpage
->document
()->find
(findEdit
->text
(), cursor,
QTextDocument::FindCaseSensitively);
} else {
cursor
= mTextpage
->document
()->find
(findEdit
->text
(), cursor.
anchor(),
QTextDocument::FindCaseSensitively);
}
mTextpage->setTextCursor(cursor);
this->setBackground();
}
So my thought was, to set the position of the cursor in the slotClose to 0, if it is -1. But this doesn't work and I definetly don't understand why.
Re: qtextcursor setposition doesn't work
Quote:
Originally Posted by
doc_symbiosis
But the cursor is set to -1 when no match is found by the find method in the findslot:
So don't put it there! Just don't call setTextCursor() if you don't want to.
Quote:
So my thought was, to set the position of the cursor in the slotClose to 0, if it is -1. But this doesn't work and I definetly don't understand why.
It doesn't work because the cursor is invalid. It's probably even not tied to your document. Use the code I gave you in my previous post instead.