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();
}
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();
}