How does QScrollArea work?
Please explain how you can work with QScrollArea
. In the designer, I did it, but it doesn't work. I didn't find any normal solutions on the Internet
1 answers
QScrollArea is a container widget for widgets, whose size is usually very large and scroll bars (sliders) are added to fit the window with them.
To put widgets in QScrollArea
, you don't need layout
(the linker), 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 window size, the sliders disappear:
QScrollArea
it can change the size of the widgets in it, stretch them. This is the responsibility of the widgetResizable property, which is False
by default.
If QScrollArea
has this property activate (mw.setWidgetResizable(True)
), then the widgets in it will stretch: