0
votes

I have a QVBoxLayout as a pointer in my window and want to add Widgets dynamically (When the user presses a button, a Widget is added to the VBoxLayout). The VBoxLayout is set as Layout in the ScrollArea. But if I add Widgets to the VBoxLayout, the size of the VBoxLayout does not change and everything gets crumpled.

  m_ControlsLayout = new QVBoxLayout; //A member variable to add things later
  {
    QPushButton *PushButton = new QPushButton(tr("Add a new control"));
    connect(PushButton, SIGNAL(clicked()), this, SLOT(addControl())); //Adds it
    m_ControlsLayout->addWidget(PushButton, 1);
  }
  for(int i=0;i<10;i++)  //I am adding 10 useless Widgets to fill the space
    addControl();

  QScrollArea *ScrollArea = new QScrollArea;
  connect(this)
  ScrollArea->setWidget(Layout2Widget(m_ControlsLayout));

The addControl() Slot just adds a Widget:

  m_ControlsLayout->addWidget(new ControlWidget);

I think I have to tell the QScrollArea that it should update its heights.

1
Have you tried scrollArea->setWidgetResizable(true)?thuga
I tried right now and if I add this, the VBoxLayout gets resized to the height of the ScrollArea. No scrollbars appear and everything is very small (or if there are less widgets, everything is stretched)feedc0de

1 Answers

0
votes

When using a scroll area to display the contents of a custom widget "ControlWidget", it is important to ensure that the size hint of the child widget is set to a suitable value. Re implement, virtual function"QSize QWidget::sizeHint ()" on your custom widget class.