Results 1 to 4 of 4

Thread: QTextCursor position

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QTextCursor position

    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:
    Qt Code:
    1. void MConsole::getInput()
    2. {
    3. emit sig_promptText(txtInput->text().mid(m_strPrompt.size()));
    4. if(txtOutput->textCursor().position() != txtOutput->textCursor().atEnd())
    5. txtOutput->textCursor().setPosition(txtOutput->toPlainText().length());
    6. txtOutput->insertPlainText("\n"+txtInput->text());
    7. txtInput->setText(m_strPrompt);
    8. txtOutput->ensureCursorVisible();
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 9th December 2007 at 19:09.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextCursor position

    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:
    Qt Code:
    1. void MConsole::getInput()
    2. {
    3. emit sig_promptText(txtInput->text().mid(m_strPrompt.size()));
    4. std::cout<<"position before:"<<txtOutput->textCursor().position()<<std::endl;
    5. if(txtOutput->textCursor().position() != txtOutput->textCursor().atEnd())
    6. {
    7. txtOutput->textCursor().setPosition(txtOutput->toPlainText().size());
    8. std::cout<<"position after:"<<txtOutput->textCursor().position()<<" size:"<<txtOutput->toPlainText().size()<<std::endl;
    9. }
    10. txtOutput->insertPlainText("\n"+txtInput->text());
    11. txtInput->setText(m_strPrompt);
    12. txtOutput->ensureCursorVisible();
    13. }
    To copy to clipboard, switch view to plain text mode 

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextCursor position

    Notice that QTextEdit::textCursor() returns a copy. To move the cursor to the end you would do:
    Qt Code:
    1. QTextCursor cursor = txtOutput->textCursor();
    2. cursor.movePosition(QTextCursor::End);
    3. txtOutput->setCursor(cursor);
    To copy to clipboard, switch view to plain text mode 
    But notice that you might just do QTextEdit::append() as well.
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    high_flyer (10th December 2007)

  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextCursor position

    Notice that QTextEdit::textCursor() returns a copy.
    Oh, of course!!
    Ah.... I should have come to that conclusion my self
    Thanks Jpn (and welcome to the team!)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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
  •  
Qt is a trademark of The Qt Company.