PDA

View Full Version : QTextCursor position



high_flyer
9th December 2007, 18:48
Hi,

a bit embarrassed to ask this, but I didn't really deal with that much text and Qt4 until now.
I need to add text to a text edit, and make sure it always is added to the end of the current text.
So in case a mouse is clicked in the QTextEdit, the cursor position will not be at the end.
All though I have been reading the docs I just don't get how the position is treated.
The only clue I found was in QTextCursor documentation:

A document can be thought of as a single string of characters with the cursor's position() being between any two characters (or at the very beginning or very end of the document).
But if I check for the cursor position and when it is not atEnd() I set the position to be the length of the current text, the position doesn't change (or it does not get to be at the end)...
I know this is stupid, but I just don't get this cursor position thing.
Any pointers to the right place in the docs will be much appreciated.

Thanks.

Here is my slot to make things clear:


void MConsole::getInput()
{
emit sig_promptText(txtInput->text().mid(m_strPrompt.size()));
if(txtOutput->textCursor().position() != txtOutput->textCursor().atEnd())
txtOutput->textCursor().setPosition(txtOutput->toPlainText().length());
txtOutput->insertPlainText("\n"+txtInput->text());
txtInput->setText(m_strPrompt);
txtOutput->ensureCursorVisible();
}

high_flyer
9th December 2007, 19:37
hmm... ok I did some debugging, and the problem is not in the way I understood the positioning (I think) but that QTextCursor::setPosition() doen't seem to work (the way I expect it to).
Here is my debug code:


void MConsole::getInput()
{
emit sig_promptText(txtInput->text().mid(m_strPrompt.size()));
std::cout<<"position before:"<<txtOutput->textCursor().position()<<std::endl;
if(txtOutput->textCursor().position() != txtOutput->textCursor().atEnd())
{
txtOutput->textCursor().setPosition(txtOutput->toPlainText().size());
std::cout<<"position after:"<<txtOutput->textCursor().position()<<" size:"<<txtOutput->toPlainText().size()<<std::endl;
}
txtOutput->insertPlainText("\n"+txtInput->text());
txtInput->setText(m_strPrompt);
txtOutput->ensureCursorVisible();
}



And here the output:


position before:10
position after:10 size:35

So it looks that although setPosition() gets 35, the position stays at 10.
Any idea what I am missing here?
If I set a position more then the strings size I get an "out of range" message in the console - the setPosition does get the right value, but why wont the cursor update to that position?

Thanks.

jpn
10th December 2007, 07:13
Notice that QTextEdit::textCursor() returns a copy. To move the cursor to the end you would do:


QTextCursor cursor = txtOutput->textCursor();
cursor.movePosition(QTextCursor::End);
txtOutput->setCursor(cursor);

But notice that you might just do QTextEdit::append() as well.

high_flyer
10th December 2007, 09:17
Notice that QTextEdit::textCursor() returns a copy.
Oh, of course!!
Ah.... I should have come to that conclusion my self :rolleyes:
Thanks Jpn (and welcome to the team!)