Results 1 to 3 of 3

Thread: Auto scroll down in QScrollArea

  1. #1
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Auto scroll down in QScrollArea

    Hi, I've made a search and found lots of answer but I still can't get it to work. I'm toying with the scroll area in which I want to add widget to it dynamically and I want the scroll area to ensure the las widget is always visible.

    I use a widget in which I use a QVBoxLayout and then set this widget in the scroll area. I can see my stuff but it always scrolls to the next to last one when I use ensureWidgetVisible. I traced this method and the size hint returned seems ok. Here's my test code:

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. #include "MainWidget.h"
    5.  
    6. int main( int argc, char* argv[] )
    7. {
    8. QApplication app( argc, argv );
    9.  
    10. // Put your own widget here.
    11. MainWidget* w = new MainWidget();
    12. w->show();
    13.  
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #pragma once
    2.  
    3. #include <QtGui>
    4.  
    5. class MainWidget : public QWidget
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. MainWidget( QWidget* parent = 0 );
    11.  
    12.  
    13. public slots:
    14. void OnNewElement();
    15.  
    16. protected slots:
    17. void ScrollToEnd();
    18.  
    19. protected:
    20. QScrollArea* mScrollArea;
    21. QWidget* mInternalWidget;
    22. QVBoxLayout* mInternalLayout;
    23. QWidget* mLastChild;
    24. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "MainWidget.h"
    2.  
    3. MainWidget::MainWidget( QWidget* parent ) : QWidget(parent)
    4. {
    5. QHBoxLayout* layout = new QHBoxLayout(this);
    6. setLayout( layout );
    7.  
    8. mScrollArea = new QScrollArea(this);
    9. layout->addWidget( mScrollArea );
    10.  
    11. mInternalWidget = new QWidget(mScrollArea);
    12. mInternalLayout = new QVBoxLayout(mInternalWidget);
    13. mInternalWidget->setLayout(mInternalLayout);
    14.  
    15. QWidget* element = NULL;
    16. for (int i=0; i<20; ++i)
    17. {
    18. QString name = QString("Base Element %0").arg(i);
    19. element = new QWidget( mInternalWidget );
    20. element->setFixedSize( 200,20 );
    21.  
    22. element->setAutoFillBackground( true );
    23. QPalette p = element->palette();
    24. p.setColor( QPalette::Window, QColor(255,0,255) );
    25. element->setPalette(p);
    26.  
    27. mInternalLayout->addWidget( element );
    28. }
    29.  
    30. mScrollArea->setWidget( mInternalWidget );
    31. mScrollArea->setWidgetResizable(true);
    32. mScrollArea->verticalScrollBar()->setSingleStep( 20 );
    33.  
    34. QPushButton* b = new QPushButton( "Add new element" );
    35. connect( b, SIGNAL(clicked()), this, SLOT(OnNewElement()) );
    36.  
    37. layout->addWidget( b );
    38. }
    39.  
    40. void MainWidget::OnNewElement()
    41. {
    42. static int count = 0;
    43. QString name = QString("New Element %0").arg(count++);
    44. QLabel* element = new QLabel( name );
    45. element->setFixedSize( 100, 20 );
    46.  
    47. element->setAutoFillBackground( true );
    48. QPalette p = element->palette();
    49. p.setColor( QPalette::Window, QColor(255,0,0) );
    50. element->setPalette(p);
    51.  
    52. mInternalLayout->addWidget( element );
    53. mLastChild = element;
    54. QTimer::singleShot( 5, this, SLOT(ScrollToEnd()) );
    55. }
    56.  
    57. void MainWidget::ScrollToEnd()
    58. {
    59. //mScrollArea->verticalScrollBar()->setValue( mScrollArea->verticalScrollBar()->maximum() );
    60. mScrollArea->ensureWidgetVisible( mLastChild );
    61. }
    To copy to clipboard, switch view to plain text mode 

    Calling ensureWidgetVisible from the OnNewElement slot does not work and I suspected that is was because the layout had not been calculated yet so I tried a singleShot timer to do the scroll. It works for the first 100 or so elements but after that, it scrolls to the next to last element again.

    I know we're on the 24th but you know the drill, coders almost always work

  2. #2
    Join Date
    Jul 2007
    Location
    California, USA
    Posts
    62
    Thanks
    17
    Thanked 7 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Auto scroll down in QScrollArea

    Did you try setting the margins = 0?

    Qt Code:
    1. mScrollArea->ensureWidgetVisible( mLastChild , 0, 0);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Auto scroll down in QScrollArea

    Yeah tried it too but it doesn't work either.

Similar Threads

  1. QTextEdit + auto scroll
    By jbpvr in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 09:30
  2. QScrollArea
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 13th December 2007, 17:20
  3. Replies: 2
    Last Post: 8th October 2006, 20:14
  4. QScrollArea's Scroll Bars
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 19th September 2006, 13:27
  5. Problem with QScrollArea updating from 4.0.1 to 4.1.0
    By SkripT in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2006, 22:35

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.