PDA

View Full Version : How to set the Text Cursor in a PlainTextEdit box?



Mia S.
27th February 2010, 13:44
I want to toggle the algebraic sign (e.g. 100 (press togglebutton) -100 (press togglebutton again) 100 ... and so on) on and off.
The Problem is that the Text Cursor in the PlainTextEdit (Read only) jumps to the beginning every time i click that button.

So the question is, how to set the (invisible) Text Cursor to the end.
Thanks 4 your help!

Here is the important part of the code. I tried serveral things (commented out by //), but without success.

---

void cal::on_algebraicsign_clicked()
{
if (aspositive==true && var!="")
{
contenttextbox = ui->textbox->toPlainText();
contenttextbox = contenttextbox.left(contenttextbox.length()-var.length());
var = "-" + var;
ui->textbox->setPlainText(contenttextbox+var);
//QPlainTextEdit::keyPressEvent(0)
aspositive=false;
}
else if (var!="")
{
contenttextbox = ui->textbox->toPlainText();
contenttextbox = contenttextbox.left(contenttextbox.length()-var.length());
var = var.right(var.length()-1);
ui->textbox->setPlainText(contenttextbox+var);
//ui->textbox->setTextCursor(contenttextbox.length()+var.length() );
//or QPlainTextEdit::moveCursor(QTextCursor.End);

aspositive=true;
}
}

---

JohannesMunk
27th February 2010, 14:39
try:

.. do your changes..

QTextCursor tc = ui->textbox->textCursor();
tc.setPosition(ui->textbox->document()->characterCount());
ui->textbox->setTextCursor(tc);

Edit: misread your question at first and thought, you wanted to save/restore the cursors position.

HIH

Johannes

Mia S.
27th February 2010, 17:30
SOLVED!
Thanks. Although that was not the solution, i got it.

void cal::set_text_cursor()
{
QTextCursor cursor1= ui->textbox->textCursor();
cursor1.atEnd();
ui->textbox->setTextCursor(cursor1);
}

and then implement that into every digit/operator (position where it has to be set to the End).

another solution is to use (puts the cursor to the end):

ui->textbox->clear();
ui->textbox->insertPlainText(contenttextbox+var);

instead of (puts the cursor to the beginning):

ui->textbox->setPlainText(contenttextbox+var);

Thanks 4 helping!