Results 1 to 5 of 5

Thread: Auto-scrolling QTextBrowser

  1. #1
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Auto-scrolling QTextBrowser

    It it possible to make a subclass of QTextBrowser that automatically scrolls to the bottom whenever it was scrolled to the bottom when text was added?

    Also, is it possible to make it stay scrolled to the same place in the text when resized?

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Auto-scrolling QTextBrowser

    1 - This QTextBrowser does it by default when text is appended. When text is inserted it doesn't but it requires very little work:
    Qt Code:
    1. MyBrowser::MyBrowser( QWidget* parent ) :
    2. QTextBrowser( parent ),
    3. atBottom( false )
    4. {
    5. connect( this, SIGNAL( textChanged() ), this, SLOT( scrollToBottom() ) );
    6. connect( this->verticalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( scrolledTo( int ) ) );
    7. }
    8.  
    9. void MyBrowser::scrollToBottom( void )
    10. {
    11. if( this->atBottom )
    12. {
    13. this->verticalScrollBar()->setValue( this->verticalScrollBar()->maximum() );
    14. }
    15. }
    16.  
    17. void MyBrowser::scrolledTo( int val )
    18. {
    19. this->atBottom = val == this->verticalScrollBar()->maximum();
    20. }
    To copy to clipboard, switch view to plain text mode 

    2 - I don't quite get your question. The browser doesn't scroll anywhere when you resize it, just show more/less text.

  3. The following user says thank you to Spitfire for this useful post:

    MTK358 (22nd January 2012)

  4. #3
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Auto-scrolling QTextBrowser

    Quote Originally Posted by Spitfire View Post
    1 - This QTextBrowser does it by default when text is appended. When text is inserted it doesn't but it requires very little work:
    Qt Code:
    1. MyBrowser::MyBrowser( QWidget* parent ) :
    2. QTextBrowser( parent ),
    3. atBottom( false )
    4. {
    5. connect( this, SIGNAL( textChanged() ), this, SLOT( scrollToBottom() ) );
    6. connect( this->verticalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( scrolledTo( int ) ) );
    7. }
    8.  
    9. void MyBrowser::scrollToBottom( void )
    10. {
    11. if( this->atBottom )
    12. {
    13. this->verticalScrollBar()->setValue( this->verticalScrollBar()->maximum() );
    14. }
    15. }
    16.  
    17. void MyBrowser::scrolledTo( int val )
    18. {
    19. this->atBottom = val == this->verticalScrollBar()->maximum();
    20. }
    To copy to clipboard, switch view to plain text mode 
    First of all, I'm inserting text using a QTextCursor, not the QTextBrowser's append method. Second, it should only auto-scroll if it was already at the bottom before text was inserted.

    Quote Originally Posted by Spitfire View Post
    2 - I don't quite get your question. The browser doesn't scroll anywhere when you resize it, just show more/less text.
    When I make it smaller horizontally, it scrolls up, and when I make it bigger horizontally, it scrolls down.

  5. #4
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Auto-scrolling QTextBrowser

    I misread your code, it actually does only autoscroll when at the bottom.

  6. #5
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Auto-scrolling QTextBrowser

    I understand that first of your problems is now solved.

    As to the second one.
    The browsers scrolls up/down when you resize it horizontally because of text wrapping feature.
    I think that unless you turn it off you won't be able to keep the text in place and turning it off doesn't make much sense.

Similar Threads

  1. Disable auto-scrolling in QPlainTextEdit
    By lukass in forum Newbie
    Replies: 2
    Last Post: 21st February 2011, 10:31
  2. auto scrolling QTextEdit
    By msmihai in forum Newbie
    Replies: 1
    Last Post: 8th January 2009, 19:08
  3. How did I disable auto-scrolling function in tree-view
    By alfa_wu in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2007, 08:31
  4. qtextbrowser stop scrolling on append
    By tpf80 in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2007, 20:28
  5. Scrolling between two widgets
    By vfernandez in forum Newbie
    Replies: 1
    Last Post: 30th October 2006, 15:39

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.