Results 1 to 4 of 4

Thread: Trouble with cursor and selecting text in QTextEdit

  1. #1
    Join Date
    Jun 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Trouble with cursor and selecting text in QTextEdit

    Qt 4.4.0
    Has only 2 methods to set position of cursor. With moveOperation = moveAnchor and keepAnchor.
    If need to select text - use keep anchor. If use moveAnchor selection is cleared (is bad, selection must be stayed while user begin selected other section - all GUI linux editors problem).
    Now i cannot store cursor position after selecting. Cursor may be only at end of selecting text or begin. For exampe, in KDevelop if select all - cursor not moved, if use QTextEdit - cursor move to end of text.
    It's impossible ? I wanted to select text and dont move cursor from initial position and i dont know how to do it.

  2. #2
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trouble with cursor and selecting text in QTextEdit

    1. Before you select some text, store the current cursor state:
    Qt Code:
    1. QTextCursor cr = textEdit->textCursor();
    To copy to clipboard, switch view to plain text mode 
    2. Select your text using movePosition and all that stuff. Do something with the selected text;
    3. Restore the saved cursor state:
    Qt Code:
    1. textEdit->setTextCursor (cr);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trouble with cursor and selecting text in QTextEdit

    Thx, but i know it.
    I want to select text and dont move cursor position.

    Just trying select text, for example all current line and dont move cursor position.
    I trying.

    Qt Code:
    1. QTextCursor cursor = textCursor();
    2. cursor.setPosition(nStartSelectionPos);
    3. cursor.setPosition(nEndSelectionPos, QTextCursor::moveAnchor);
    4. setTextCursor(cursor);
    To copy to clipboard, switch view to plain text mode 

    Yeah and cursor was stayed in end of selection text.
    If do it, like this :
    Qt Code:
    1. QTextCursor cursor = textCursor();
    2. int nCurPos = cursor.position();
    3. cursor.setPosition(nStartSelectionPos);
    4. cursor.setPosition(nEndSelectionPos, QTextCursor::moveAnchor);
    5. cursor.setPosition(nCurPos);
    6. setTextCursor(cursor);
    To copy to clipboard, switch view to plain text mode 
    No selected text, because cursor is moving - its my problem.

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trouble with cursor and selecting text in QTextEdit

    Try creating a cursur like that
    QTextCursor c(document);

    where document is your QTextDocument*


    If that makes no difference:
    call position() on the cursor before doing the selection, and restore (only) the position when you're done with setPosition.



    Not sure if it will work, but worth a try.

    Good luck!

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.