Results 1 to 9 of 9

Thread: How do I set a QTextEdit to a specific line by line number?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question How do I set a QTextEdit to a specific line by line number?

    Hello.
    In my application I'm writing in Qt, I am trying to implement a "Go to Line" dialog which will set the central widget (a QTextEdit) to a specific line that the user enters, using a line number.

    However, I have searched the docs for such a function will allow one to easily do this, and have not found one. Is this because it does not exist? Or maybe I missed it?

    I also need to display the line numbers in the QTextEdit like many text editors do (i.e. displaying the line numbers on the left side of each line), but have also failed to find such existing functionality.

    Any suggestions??

    Thanks.

    EDIT:

    Here is what I have so far:

    Qt Code:
    1. void C_MainWindow::goToLineAction() {
    2.  
    3. bool ok;
    4. int line_number = QInputDialog::getInt(this, tr("Go to Line"),
    5. tr("Enter a line number to go to: "), 1, 1, central_widget_TextDocument->blockCount(), 1, &ok);
    6. if (ok) {
    7. }
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    This creates the "Go to Line" dialog with the default value being the first line's number (1) and the minimal value also being 1, and the maximal value being the number of "blocks" (which I understand is really lines, I believe) that the text editor's document currently has. But it does not (yet) actually go to the line in the QTextEdit, since I don't know how to do that yet (hence this thread).
    Last edited by Coolname007; 27th December 2012 at 22:39.

  2. #2
    Join Date
    Jul 2012
    Location
    Switzerland
    Posts
    32
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I set a QTextEdit to a specific line by line number?

    Well I would try to set the QTextCursor to this position, something like:

    Qt Code:
    1. QTextCursor txtCursor = edit->textCursor();
    2. //position where you want it
    3. txtCursor.setPosition(...);
    4. edit->setTextCursor(txtCursor);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Read a specific line from a file
    By rleojoseph in forum Qt Programming
    Replies: 11
    Last Post: 21st March 2011, 11:58
  2. Replies: 1
    Last Post: 22nd July 2010, 17:12
  3. QTextDocument line number
    By bunjee in forum Qt Programming
    Replies: 14
    Last Post: 10th June 2008, 13:49
  4. Line Number - QTextEdit...???
    By deepusrp in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 16:34
  5. How to get column number in a line
    By showhand in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2006, 09:42

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.