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