Python Forum
[PyQt] QScrollArea does not work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] QScrollArea does not work
#3
You added a widget to the scroll widget window, but not in the correct way. Use scroll_area.setWidget(scrolled_widget).

You should also use layout managers instead of setGeometry. Run the attached program and see how easily it adapts to changes in window size. This would be very difficult if you placed and resized all the widgets yourself, but it happens automatically when you use a layout manager.
import sys
from PySide6.QtWidgets import (
    QApplication,
    QWidget,
    QScrollArea,
    QLabel,
    QVBoxLayout,
    QPushButton,
)


text = (
    "The scroll bars appearance depends on the currently set scroll bar policies.  "
    "You can control the appearance of the scroll bars using the inherited functionality "
    "from QAbstractScrollArea.\n"
    "\n"
    "For example, you can set the horizontalScrollBarPolicy and verticalScrollBarPolicyproperties. "
    "Or if you want the scroll bars to adjust dynamically when the contents of the scroll area "
    "changes, you can use the horizontalScrollBar() and verticalScrollBar() functions "
    "(which enable you to access the scroll bars) and set the scroll bars’ values whenever the "
    "scroll area’s contents change, using the setValue() function.\n"
    "\n"
    "You can retrieve the child widget using the widget() function. The view can be made to be "
    "resizable with the setWidgetResizable() function. The alignment of the widget can be "
    "specified with setAlignment().\n"
)


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.pane = QWidget()
        self.view = QScrollArea()
        self.view.setWidget(self.pane)
        self.view.setWidgetResizable(True)
        layout = QVBoxLayout(self)
        layout.addWidget(self.view)

        self.text = QLabel(text)
        self.text.setWordWrap(True)
        self.text.setMinimumWidth(300)
        self.button = QPushButton(text="Push Me")
        layout = QVBoxLayout(self.pane)
        layout.addWidget(self.text)
        layout.addWidget(self.button)


app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
Reply


Messages In This Thread
QScrollArea does not work - by HeinKurz - Jan-24-2023, 05:23 PM
RE: QScrollArea does not work - by Axel_Erfurt - Jan-24-2023, 06:56 PM
RE: QScrollArea does not work - by deanhystad - Jan-24-2023, 07:38 PM
RE: QScrollArea does not work - by HeinKurz - Jan-24-2023, 09:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] How do I get a QScrollArea to scroll? LavaCreeperKing 9 8,038 Oct-29-2021, 08:33 AM
Last Post: Axel_Erfurt
  [PyQt] QScrollArea with a gridlayout littleGreenDude 2 10,061 Jan-28-2019, 07:14 PM
Last Post: littleGreenDude

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020