Results 1 to 3 of 3

Thread: Making arrow keys change cursor position in QTextEdit object

  1. #1
    Join Date
    Oct 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Making arrow keys change cursor position in QTextEdit object

    Qt 5.5.1

    My objective is to display the line and column of the cursor in a QTextEdit object. It works properly using keys like enter and backspace to navigate, but the arrow keys seem to move the caret without changing the cursor position (the display for line and column number will stay the same despite the caret moving elsewhere for this reason). How can I make it so the arrow keys change the cursor position so I can update my label?

    Here is my code that updates the display when the cursor position changes:
    Qt Code:
    1. QTextCursor cursor = ui->textEdit->textCursor();
    2.  
    3. // ...
    4.  
    5. void MainWindow::on_textEdit_cursorPositionChanged()
    6. {
    7. ui->statusBar->showMessage("Line " + QString::number(cursor.blockNumber() + 1)
    8. + ", Col " + QString::number(cursor.positionInBlock() + 1));
    9. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Making arrow keys change cursor position in QTextEdit object

    Interesting, as far as I can tell the cursor position should be updated.

    Have you tried connecting to the QTextDocument::cursorPositionChanged() signal instead?
    You get the document through QTextEdit::document().

    Cheers,
    _

    P.S.: General recommendation: avoid using "connect by name", it can easily when you rename objects in designer.
    Always prefer explicit connects.

  3. #3
    Join Date
    Oct 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Making arrow keys change cursor position in QTextEdit object

    I got exactly the same behavior after connecting that way.

Similar Threads

  1. QTextEdit restore cursor position
    By artur231 in forum Qt Programming
    Replies: 3
    Last Post: 15th May 2014, 22:02
  2. Replies: 0
    Last Post: 19th July 2013, 08:05
  3. Replies: 3
    Last Post: 3rd September 2012, 07:32
  4. Replies: 2
    Last Post: 20th February 2012, 21:58
  5. Restoring cursor position in QTextEdit
    By s.toonen in forum Qt Programming
    Replies: 2
    Last Post: 30th May 2008, 14:44

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.