Home python How does QScrollArea work?

How does QScrollArea work?

Author

Date

Category

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:

 enter image description here

When you increase the size of the window, the sliders will disappear:

 enter image description here


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:

 enter image description here

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions