Results 1 to 5 of 5

Thread: QTextEdit Qt4: simple question

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QTextEdit Qt4: simple question

    Hello all,
    I'm filling a QTextEdit with a load of text in on go using
    ->setPlainText(allTheText);
    So, here's the silly question ... How do I now go to the end of the text? i.e. scroll all the way down to the last line in the text? Is there a QTextEdit::goToEnd() or whatever?

    thanks
    K

  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: QTextEdit Qt4: simple question

    From QTextEdir docs:
    Selection of text is handled by the QTextCursor class, which provides functionality for creating selections, retrieving the text contents or deleting selections. You can retrieve the object that corresponds with the user-visible cursor using the textCursor() method. If you want to set a selection in QTextEdit just create one on a QTextCursor object and then make that cursor the visible cursor using setCursor(). The selection can be copied to the clipboard with copy(), or cut to the clipboard with cut(). The entire text can be selected using selectAll().
    bool QTextCursor::movePosition ( MoveOperation op, MoveMode mode = MoveAnchor, int n = 1 )

    Moves the cursor in accordance with the MoveOperation op, using MoveMode mode. The move is performed n (default 1) times.

    If mode is KeepAnchor, the cursor selects the text it moves over. This is the same effect that the user achieves when they hold down the Shift key and move the cursor with the cursor keys.
    QTextCursor::End 11 Move to the end of the document.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTextEdit Qt4: simple question

    Sorry, that's not exactly what I mean. I can move the cursor to the end of the text with ...

    Qt Code:
    1. QTextCursor cursor = mainView->textCursor();
    2. cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
    To copy to clipboard, switch view to plain text mode 
    where mainView is my QTextEdit.

    This is the problem:
    I load some large text into the QTextEdit with ...
    Qt Code:
    1. QString allTheText;
    2. //...fill the string here...
    3. //put the string into the mainView
    4. mainView->setPlainText(allTheText);
    To copy to clipboard, switch view to plain text mode 

    Now my QTextEdit has more text in it than can be shown in the visible area. There is a vertical scroll bar on the right with which I can scroll down to the end of the text. However, I'd like my QTextEdit to scroll down for me when I load the text.
    See, my texts are log files and when I run my app it should auto-load the newest one and display the most recent data in the file - i.e. the end of the QTextEdit. It would be handy if didn't have to always scroll down through the files.

    maybe I'm missing something obvious
    Thanks
    K

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit Qt4: simple question

    I think this will be the most convinient.

    void QTextEdit::scrollToAnchor ( const QString & name ) [slot]
    Scrolls the text edit so that the anchor with the given name is visible; does nothing if the name is empty, or is already visible, or isn't found.
    BTW. It should work using QTextCursor too. Did you try it? Just remember to set the cursor back when you change it.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTextEdit Qt4: simple question

    ok guys, thanks, I've got it. It's a different solution to a different problem though.

    This is what I'm doing....

    Qt Code:
    1. //put the string into the mainView
    2. mainView->setPlainText(allTheText);
    3.  
    4. //move the cursor to the end of the text
    5. QTextCursor cursor(mainView->textCursor());
    6. cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
    7. //I have to do this too. - I was forgetting this!!!!
    8. mainView->setTextCursor(cursor);
    To copy to clipboard, switch view to plain text mode 

    as you guys say ... this should work. And it does ... usually.

    However, I had another problem too. I was making my QTextEdit in my mainWindow constructor - and I was loading my file and displaying it from the constructor too. That wasn't moving my cursor.
    now I have a
    Qt Code:
    1. QTimer::singleShot(0, this, SLOT(init()));
    To copy to clipboard, switch view to plain text mode 
    and I load my file from init()

    That works fine.

    thanks for your help
    Kevin

Similar Threads

  1. a simple noob question
    By wrproject in forum Newbie
    Replies: 1
    Last Post: 2nd July 2008, 00:25
  2. Simple question about QTextEdit
    By maverick_pol in forum Qt Programming
    Replies: 1
    Last Post: 6th March 2008, 18:52
  3. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 01:01
  4. a question about visualizing cursor in read-only QTextEdit
    By kennyxing in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2006, 20:52
  5. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 23:37

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.