How do i resize the contents of a QScrollArea as more widgets are placed inside

I have a QScrollArea Widget, which starts empty;

It has a vertical layout, with a QGridLayout, and a vertical spacer to keep it at the top, and prevent it from stretching over the whole scroll area;

Elsewhere in the program, there is a QTextEdit, which when changed, has its contents scanned for "species" elements, and then they are added to the QGridLayout. Any species elements which have been removed are removed too. This bit works;

I have turned the vertical scrollbar on all the time, so that when it appears it does not sit on top of the other stuff in there. Note that the scroll bar is larger than the scroll box already though, despite not needing to be.

This is the problem. The scroll area seems to be preset, and i cannot change it. If i add more rows to the QGridLayout, the scroll area doesn't increase in size.

Instead, it stays the same size, and squeezes the QGridLayout, making it look ugly (at first);

And then after adding even more it becomes unusable;

Note that again, the scroll bar is still the same size as in previous images. The first two images are from Qt Designer, the subsequent 3 are from the program running.

If I resize the window so that the QScrollArea grows, then I see this:

Indicating that there's some layout inside the scroll area that is not resizing properly.

My question is; what do I need to do to make the scrollable area of the widget resize dynamically as I add and remove from the QGridLayout?

Answers


The documentation provide an answer :

widgetResizable : bool This property holds whether the scroll area should resize the view widget. If this property is set to false (the default), the scroll area honors the size of its widget.

Set it to true.


If you're coming here from Google and not having luck with the accepted answer, that's because you're missing the other secret invocation: QScrollArea::setWidget. You must create and explicitly identify a single widget which is to be scrolled. It's not enough to just add the item as a child! Adding multiple items directly to the ScrollArea will also not work.

This script demonstrates a simple working example of QScrollArea:

from PySide.QtGui import *

app = QApplication([])

scroll = QScrollArea()
scroll.setWidgetResizable(True) # CRITICAL

inner = QFrame(scroll)
inner.setLayout(QVBoxLayout())

scroll.setWidget(inner) # CRITICAL

for i in range(40):
    b = QPushButton(inner)
    b.setText(str(i))
    inner.layout().addWidget(b)

scroll.show()
app.exec_()

Why don't you use a QListView for your rows, it will manage all the issues for you? Just make sure that after you add it you click on the Class (top right window of designer) and assign a layout or it wont expand properly.

I use a QLIstWidget inside a QScrollArea to make a scrollable image list

Try this for adding other objects to the list, this is how I add an image to the list.

QImage& qim = myclass.getQTImage();

QImage iconImage = copyImageToSquareRegion(qim, ui->display_image->palette().color(QWidget::backgroundRole()));

QListWidgetItem* pItem = new QListWidgetItem(QIcon(QPixmap::fromImage(iconImage)), NULL);

pItem->setData(Qt::UserRole, "thumb" + QString::number(ui->ImageThumbList->count()));  // probably not necessary for you

QString strTooltip = "a tooltip"

pItem->setToolTip(strTooltip);

ui->ImageThumbList->addItem(pItem);

Need Your Help

How to set maxlength for multiline TextBox?

asp.net textbox

When using a MultiLine TextBox (which generates a TextArea) setting the MaxLength property has no effect. What is the best workaround? I'd like to get basic, intended functionality with minimum of ...

Exclude menu item from the collapse of bootstrap 3 navbar

html css twitter-bootstrap twitter-bootstrap-3

Here's what I have, I've tried moving around my section inside the "brand" and do a pull-right, outside the brand and outside the collapse and do a pull-left/right, while also trying to place it be...