Results 1 to 5 of 5

Thread: To jump to a specific line in a textedit

  1. #1
    Join Date
    Nov 2013
    Location
    Chandigarh, India
    Posts
    62
    Thanks
    8
    Thanked 11 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default To jump to a specific line in a textedit

    Can anyone suggest me some code to jump to a specific line of a text edit widget. I have tried using blockNumber() function through QTextCursor but this returns only the current line where cursor is placed. I want to set the cursor to a specific line. Can someone help me in this?

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: To jump to a specific line in a textedit

    You can iterate QTextDocument rootframe and count line, and having cursor position from each block

    RootDocFrame = doc->rootFrame();

    sample code
    bool RTF::Writer::write(QIODevice* device, QTextDocument* text)
    on file https://manual-indexing.googlecode.c...rtf/writer.cpp writing rtf format.. parse all line and element..

    or from event or findBlockByLineNumber(line_number));

    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. QTextCursor text_cursor(central_widget_TextDocument->findBlockByLineNumber(line_number));
    8. text_cursor.select(QTextCursor::BlockUnderCursor);
    9. central_widget_TextEdit->setTextCursor(text_cursor);
    10. }
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2013
    Location
    Chandigarh, India
    Posts
    62
    Thanks
    8
    Thanked 11 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: To jump to a specific line in a textedit

    I tried this set of code but it gives an error that QTextEdit has an unknown member function findBlockByLineNumber. The element central_widget_TextDocument is a textedit only? .. And can you also tell me is C_MainWindow::goToLineAction() a pre-defined function or a user-defined function?

  4. #4
    Join Date
    Oct 2011
    Posts
    27
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To jump to a specific line in a textedit

    Hi,

    this is what i've done on a previous project from a QPlainTextEdit :

    Qt Code:
    1. class MyTxtEdit : public QPlainTextEdit
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit MyTxtEdit(QWidget *parent = 0);
    6.  
    7. int currentLine() const
    8. {
    9. return textCursor().blockNumber()+1;
    10. }
    11.  
    12. public slots:
    13. void setCurrentLine(int line)
    14. {
    15. QTextCursor cursor = textCursor();
    16.  
    17. int moves = line-currentLine();
    18.  
    19. if(moves)
    20. {
    21. if(moves<0) cursor.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor, -moves);
    22. else cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, moves);
    23.  
    24. cursor.movePosition(QTextCursor::StartOfLine);
    25.  
    26. setTextCursor(cursor);
    27. }
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    but the text contents were really simple (raw text, no images, ...), so, in your case, maybe the "currentLine()" method won't give you a correct value.

  5. #5
    Join Date
    Nov 2013
    Location
    Chandigarh, India
    Posts
    62
    Thanks
    8
    Thanked 11 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: To jump to a specific line in a textedit

    It worked.. Thank You very much...

Similar Threads

  1. Line Numbering in textedit
    By aaditya190 in forum Newbie
    Replies: 2
    Last Post: 15th November 2013, 09:58
  2. Replies: 5
    Last Post: 15th July 2013, 11:36
  3. How do I set a QTextEdit to a specific line by line number?
    By Coolname007 in forum Qt Programming
    Replies: 8
    Last Post: 1st February 2013, 06:18
  4. TextEdit with one line
    By alizadeh91 in forum Qt Quick
    Replies: 4
    Last Post: 3rd November 2012, 22:53
  5. How can I paint a line in TextEdit on QT-4.3.2?
    By cslijy in forum Qt Programming
    Replies: 2
    Last Post: 23rd November 2007, 09:08

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.