2

Making my way up the Qt learning curve, I've seen many questions about dynamic layouts but the solutions aren't working for me or I don't quite understand them.

Reference questions:: Qt Scroll Area does not add in scroll bars, How can i make widgets overflow to make a scrollbar appear in Qt?

Question:: I want to have a dynamic layout of a set of widgets within a QScrollArea. I've been able to do this manually in Qt Creator and now I am trying to do it through code.

  • How do I prevent the widgets from stretching/force the area to scroll?

  • How do I have the added widgets start from the top? I have a vertical spacer in my QVBoxLayout but that pushes everything to the bottom.


Simple test code::

void MainWindow::on_pushButton_clicked()
{
    ui->myScroll->setWidgetResizable(true); //making sure this is set
    QPushButton *b = new QPushButton(this);
    b->setText(QString("Hello Button"));
    QHBoxLayout *h = new QHBoxLayout();
    h->addWidget(b,0);
    ui->myVBoxLayout->addLayout(h,0);

}

Result:: Left side squished (dynamic) – Right side Ok (set up manually)

enter image description here


Qt Creator Setup:: Left side: dynamic – Right side set up manually

enter image description here

Properties::

enter image description here

Community
  • 1
  • 1
spring
  • 18,009
  • 15
  • 80
  • 160

1 Answers1

1

You can set use setMinimumHeight() on your buttons for preventing squished buttons. The layout can be configured with setContentsMargin() for space between item-border and item-content (QtDesigner has all four directions set to 9 IIRC) and setSpacing() for space between items (QtDesigner uses a default of 6). Also setWidgetResizable(true) allows your scrollarea to resize the view widgeth inside the area (this is where your layout and children are being placed).

This works for me:

In constructor or code set scrollArea->widget() to hold the QVBoxLayout:

v = new QVBoxLayout;
ui->scrollArea->widget()->setLayout(v);

In Button Slot:

void MainWindow::pushButtonPressed()
{
    ui->scrollArea->setWidgetResizable(true);
    QPushButton *b = new QPushButton(this);
    b->setText(QString("Hello Button"));
    QHBoxLayout *h = new QHBoxLayout();
    h->addWidget(b,0);
    v->addLayout(h);
}
Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38
  • Thanks - tried setting `setMinimumHeight()` for the buttons but they are still squished, even with `setWidgetResizable(true)` on the scrollArea. I think the issue is the scrollArea not resizing but I don't know what else to try. – spring Aug 14 '13 at 05:11
  • `setMinimumHeight()` doesn't seem to work correctly for buttons. What I've done is to add another QWidget (maximum width of 1) with the desired minimum height left or right to the button it self. This causes the buttons to respect it's minimum height. – bkausbk Aug 14 '13 at 06:02
  • Is your QVBoxLayout set to be the layout of QScrollArea->widget()? The layout needs to be the layout of the view, not the scrollarea itself, since actually the view is growing to be scrolled, but the scrollarea itself stays at given size. – Sebastian Lange Aug 14 '13 at 06:22
  • Hey Sebastian - thanks, with your help and some fiddling I finally got the "squished" problem resolved. I'm still stuck on the buttons not being added from the top down. The layout divides the space equally as buttons are added. Tried setting TopAlign on the layout and various spacing options. Any idea how to force buttons to present from top down? – spring Aug 15 '13 at 02:27
  • What does your ``direction()`` from your scrollarea->widget->layout() look like? In default setup buttons are added to the end. Try ``QBoxLayout::setDirection(QBoxLayout::TopToBottom)`` – Sebastian Lange Aug 15 '13 at 04:57
  • Will try that. Got some additional help and using `v->setSizeConstraint(QLayout::SetFixedSize);` was suggested - that works. – spring Aug 15 '13 at 05:00