PDA

View Full Version : Trouble with cursor and selecting text in QTextEdit



R_Torsten
6th June 2008, 11:30
Qt 4.4.0
Has only 2 methods to set position of cursor. With moveOperation = moveAnchor and keepAnchor.
If need to select text - use keep anchor. If use moveAnchor selection is cleared (is bad, selection must be stayed while user begin selected other section - all GUI linux editors problem).
Now i cannot store cursor position after selecting. Cursor may be only at end of selecting text or begin. For exampe, in KDevelop if select all - cursor not moved, if use QTextEdit - cursor move to end of text.
It's impossible ? I wanted to select text and dont move cursor from initial position and i dont know how to do it.

roxton
6th June 2008, 19:59
1. Before you select some text, store the current cursor state:


QTextCursor cr = textEdit->textCursor();

2. Select your text using movePosition and all that stuff. Do something with the selected text;
3. Restore the saved cursor state:


textEdit->setTextCursor (cr);

R_Torsten
6th June 2008, 21:30
Thx, but i know it.
I want to select text and dont move cursor position.

Just trying select text, for example all current line and dont move cursor position.
I trying.



QTextCursor cursor = textCursor();
cursor.setPosition(nStartSelectionPos);
cursor.setPosition(nEndSelectionPos, QTextCursor::moveAnchor);
setTextCursor(cursor);


Yeah and cursor was stayed in end of selection text.
If do it, like this :


QTextCursor cursor = textCursor();
int nCurPos = cursor.position();
cursor.setPosition(nStartSelectionPos);
cursor.setPosition(nEndSelectionPos, QTextCursor::moveAnchor);
cursor.setPosition(nCurPos);
setTextCursor(cursor);

No selected text, because cursor is moving - its my problem.

caduel
7th June 2008, 19:17
Try creating a cursur like that
QTextCursor c(document);

where document is your QTextDocument*


If that makes no difference:
call position() on the cursor before doing the selection, and restore (only) the position when you're done with setPosition.



Not sure if it will work, but worth a try.

Good luck!