Results 1 to 3 of 3

Thread: How to set the Text Cursor in a PlainTextEdit box?

  1. #1
    Join Date
    Feb 2010
    Location
    Northern Germany
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default How to set the Text Cursor in a PlainTextEdit box?

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

    ---

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set the Text Cursor in a PlainTextEdit box?

    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
    Last edited by JohannesMunk; 27th February 2010 at 14:44.

  3. The following user says thank you to JohannesMunk for this useful post:

    Mia S. (27th February 2010)

  4. #3
    Join Date
    Feb 2010
    Location
    Northern Germany
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to set the Text Cursor in a PlainTextEdit box?

    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!

Similar Threads

  1. QGraphicsTextItem and text cursor position via QPoint
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 10th July 2017, 19:38
  2. QGraphicsTextItem: How to show the text cursor?
    By breezeight in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2009, 17:44
  3. Is there a way to get text from under the mouse cursor?
    By squirrel in forum Qt Programming
    Replies: 1
    Last Post: 12th July 2009, 02:39
  4. QTextEdit current cursor text format
    By afail in forum Qt Programming
    Replies: 0
    Last Post: 13th April 2009, 15:24
  5. How to synchronize text cursor and cursor in QTextEdit
    By kennyxing in forum Qt Programming
    Replies: 6
    Last Post: 18th February 2007, 10:14

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.