PDA

View Full Version : qtextcursor setposition doesn't work



doc_symbiosis
5th October 2012, 16:07
Hello,

in a program I have the following code:

void FindWidget::slotClose() {
cursor = mTextpage->textCursor();
qDebug() << "cursor position on close start: " << cursor.position();
if ( cursor.position() == -1) {
cursor.setPosition(QTextCursor::Start,QTextCursor: :MoveAnchor);
qDebug() << "cursor position after setPosition: " << cursor.position();

mTextpage->setTextCursor(cursor);
}
mTextpage->setFocus();
close();
}

Example output is the following


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:


QTextCursor cursor = mTextpage->textCursor();

Also, same result.

Thanks in advance for any help.

sonulohani
8th October 2012, 05:16
You're not changing the position of the cursor. For this:-



cursor.setPosition(int);

doc_symbiosis
8th October 2012, 13:52
but in line 5, I have


cursor.setPosition(QTextCursor::Start,QTextCursor: :MoveAnchor);

I also tried the following line instead:


cursor.setPosition(0);

sonulohani
9th October 2012, 08:03
Oh! I didnt see that.


void FindWidget::slotClose() {
cursor = mTextpage->textCursor();
qDebug() << "cursor position on close start: " << cursor.position();
if ( cursor.position() == -1) {
cursor.movePosition(QTextCursor::Start,QTextCursor ::MoveAnchor); // Use move position instead of setPosition
qDebug() << "cursor position after setPosition: " << cursor.position();

mTextpage->setTextCursor(cursor);
}
mTextpage->setFocus();
close();
}

got it or not.
Dont forget to hit thanks button.

doc_symbiosis
9th October 2012, 19:11
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:


cursor.movePosition(QTextCursor::Start,QTextCursor ::MoveAnchor);
cursor.movePosition(QTextCursor::Start);
cursor.setPosition(QTextCursor::Start);
cursor.setPosition(0);

In line 2, I use


QTextCursor cursor = mTextpage->textCursor();

I've added the .cpp and the .h file. Perhaps the I missed something else, what's wrong.

wysota
9th October 2012, 21:24
Could you state what is the result you are trying to achieve?

doc_symbiosis
10th October 2012, 07:32
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.

wysota
10th October 2012, 08:22
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.


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.

QTextCursor cursor(textEdit->document());
textEdit->setTextCursor(cursor);

doc_symbiosis
10th October 2012, 08:37
But the cursor is set to -1 when no match is found by the find method in the findslot:


void FindWidget::slotFind()
{
QTextCursor cursor = mTextpage->textCursor();

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.

wysota
10th October 2012, 08:43
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.


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.