Please explain how you can work with QScrollArea
. I did it in the designer, but it doesn’t work. I couldn’t find normal solutions on the Internet
Answer 1, authority 100%
QScrollArea is a container widget for widgets, which are usually very large and to make the window fit with them, scroll bars (sliders) are added.
To place widgets in a QScrollArea
does not need a layout
, but a widget, so it has a setWidget
method.
For example, this code will create a large number of buttons:
from PyQt5.QtWidgets import QApplication, QGridLayout, QPushButton, QWidget, QScrollArea
app = QApplication ([])
layout = QGridLayout ()
for i in range (10):
for j in range (5):
button = QPushButton (f '{i} x {j}')
layout.addWidget (button, i, j)
w = QWidget ()
w.setLayout (layout)
mw = QScrollArea ()
mw.setWidget (w)
mw.resize (200, 200)
mw.show ()
app.exec ()
The window will look like this:
When you increase the size of the window, the sliders will disappear:
QScrollArea
can resize widgets in it, stretch them. Responsible for this is the widgetResizable property, which is by default False
.
If QScrollArea
activates this property (mw.setWidgetResizable (True)
), then the widgets in it will stretch: