Skip to content

Commit 319cafe

Browse files
committed
Added scrollable container for Qt5.
1 parent ad18e49 commit 319cafe

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#! /usr/bin/python3 -B
2+
3+
import itertools
4+
import PyQt5.QtCore as QtCore
5+
import PyQt5.QtWidgets as QtWidgets
6+
7+
from ScrollableContainers.Qt5 import ScrollableAreaQt5
8+
9+
###############################################################################
10+
11+
def grid_of_widgets():
12+
app = QtWidgets.QApplication(['`ScrollableAreaQt5` demo'])
13+
window = QtWidgets.QMainWindow()
14+
15+
# Create a scrollable area.
16+
scrollable_area = ScrollableAreaQt5()
17+
18+
# Add widgets to the `area` attribute of the scrollable area, not to the
19+
# scrollable area itself.
20+
dim = 10
21+
grid_layout = QtWidgets.QGridLayout(scrollable_area.area)
22+
for (i, j) in itertools.product(range(dim), repeat=2):
23+
grid_layout.addWidget(QtWidgets.QLabel(text=f'Label\n({i}, {j})'), i, j)
24+
25+
window.setCentralWidget(scrollable_area)
26+
window.show()
27+
app.exec_()
28+
29+
###############################################################################
30+
31+
def single_widget():
32+
app = QtWidgets.QApplication(['`ScrollableAreaQt5` demo'])
33+
window = QtWidgets.QMainWindow(size=QtCore.QSize(600, 200))
34+
35+
scrollable_area = ScrollableAreaQt5()
36+
37+
vbox = QtWidgets.QVBoxLayout(scrollable_area.area)
38+
vbox.addWidget(QtWidgets.QLabel(text='big window, small label'))
39+
40+
window.setCentralWidget(scrollable_area)
41+
window.show()
42+
app.exec_()
43+
44+
###############################################################################
45+
46+
if __name__ == '__main__':
47+
grid_of_widgets()
48+
single_widget()

src/ScrollableContainers/Qt5.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
__all__ = ['ScrollableAreaQt5']
2+
3+
import PyQt5.QtCore as QtCore
4+
import PyQt5.QtWidgets as QtWidgets
5+
6+
7+
class ScrollableAreaQt5(QtWidgets.QScrollArea):
8+
"""
9+
Container with horizontal and vertical scrolling capabilities. Widgets must be
10+
added to its `area` attribute.
11+
"""
12+
13+
def __init__(self, *args, **kwargs):
14+
super().__init__(*args, **kwargs)
15+
16+
# This object only allows a container to be placed in it.
17+
container = QtWidgets.QWidget()
18+
self.setWidget(container)
19+
vbox = QtWidgets.QVBoxLayout(container)
20+
self.area = QtWidgets.QWidget()
21+
vbox.addWidget(self.area, alignment=QtCore.Qt.AlignHCenter)
22+
vbox.addStretch()
23+
self.setWidgetResizable(True)

0 commit comments

Comments
 (0)