PyQt5 qscrollarea
PyQt5’s QScrollArea component is a control container that can hold more components. If there are too many components, a scrollbar will appear, and the scrollbar will change according to the size of the container and the size of the components inside.
The pyqt5 qscrollarea is a great way to add scrolling functionality to your Qt applications. It is easy to use and can be customized to fit your needs. You can use the qscrollarea to create custom widgets, scrollable lists, and more.
qscrollarea example
The usage can be seen in the following example
import sys
from PyQt5.QtWidgets import *
class test(QWidget):
def __init__(self):
super(). __init__()
self.initui()
def initui(self):
# create box layouts
la=QHBoxLayout()
lb=QVBoxLayout()
lc=QHBoxLayout()
# create scrollable area
scroll=QScrollArea()
a=QWidget()
a.setLayout(lb)
lb.addLayout(lc)
for x in range(50):
lb.addWidget(QPushButton(str(x)))
for x in range(50):
lc.addWidget(QPushButton(str(x)))
# set scroll size and add widget
scroll.setMinimumSize(200,200)
#scrollarea As a component, you can set the window
scroll.setWidget(a)
la.addWidget(scroll)
self.setLayout(la)
self.show()
if __name__=='__main__':
app=QApplication(sys.argv)
win=test()
sys.exit(app.exec_())
The QtWidgets.QScrollArea class provides a scrolling view onto another widget. This can be used to ensure that child widgets are visible within the viewport even if they would otherwise fall outside of it. In addition, QScrollArea provides scroll bars and several policy options that can be set to control how the scrolling area reacts to changes in the size of its contents.
If you have a widget (e.g., a QTextEdit) that is too large to fit into the available space, you can use a QScrollArea to make sure all its content is accessible. The simplest way to use a QScrollArea is to place it inside a layout and add your widget(s) to it.
qcrollarea layout
The QScrollArea scroll area control class provides a scrolling view of another window, a scroll area is usually used to display a child window.
If the content of the child window exceeds the size of the displayed window, then QSrollArea automatically provides a scrollbar that can be manipulated to allow the user to view the entire image or manipulate all controls in the window.
QSrollArea can add scrollbars to any QWidget control.
The common methods of QScrollArea are.
- setWidget(): sets the control to be a child of QScrollArea.
- takeWidget(): removes a child control of QScrollArea.
- widget(): return the child control of QScrollArea.
- setWidgetResizable(): set to true, the scrollArea widget will be automatically adjusted to avoid scrollbars that can be left out, or to use extra space.
- widgetResizable(): get the setting whether the controls in the area are automatically adjustable or not.
- ensureVisible(): ensure that a certain area is visible, scrolling if necessary.
- ensureWidgetVisible(): ensure that the specified control widget is visible, scrolling if necessary.
qscrollarea add widget
Create the file qscrollarea.py, set two scroll areas horizontally to demonstrate image scrolling display and control scrolling respectively.
You can combine qscrollarea with many different widgets and add widgets inside the qscrollarea.
The complete code is as follows:
import sys,os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (QApplication, QWidget, QSplitter, QVBoxLayout,
QGroupBox, QScrollArea, QRadioButton, QCheckBox,
QLabel)
from PyQt5.QtGui import QPixmap,QPalette
from PyQt5.QtCore import Qt
class DemoScrollArea(QWidget):
def __init__(self, parent=None):
super(DemoScrollArea, self).__init__(parent)
# Set window title
self.setWindowTitle('PyQt5: QScrollArea Demo!')
# Resize window
self.resize(460, 350)
self.initUi()
def initUi(self):
mainLayout = QVBoxLayout(self)
hSplitter = QSplitter(Qt.Horizontal)
saLeft = QScrollArea(self)
disp_img = QLabel(self)
disp_img.setPixmap(QPixmap(os.path.dirname(__file__) + '/cat.png'))
saLeft.setBackgroundRole(QPalette.Dark)
saLeft.setWidget(disp_img)
saRight = QScrollArea(self)
#Add widgets
scrollAreaWidgetContents = QWidget()
vLayout = QVBoxLayout(scrollAreaWidgetContents)
vLayout.addWidget(self.createFirstExclusiveGroup())
vLayout.addWidget(self.createSecondExclusiveGroup())
vLayout.addWidget(self.createNonExclusiveGroup())
scrollAreaWidgetContents.setLayout(vLayout)
saRight.setWidget(scrollAreaWidgetContents)
hSplitter.addWidget(saLeft)
hSplitter.addWidget(saRight)
mainLayout.addWidget(hSplitter)
self.setLayout(mainLayout)
def createFirstExclusiveGroup(self):
groupBox = QGroupBox('Radio Buttons', self)
radio1 = QRadioButton('&Radio Button 1', self)
radio1.setChecked(True)
radio2 = QRadioButton('R&adio button 2', self)
radio3 = QRadioButton('Ra&dio button 3', self)
vLayout = QVBoxLayout(groupBox)
vLayout.addWidget(radio1)
vLayout.addWidget(radio2)
vLayout.addWidget(radio3)
vLayout.addStretch(1)
groupBox.setLayout(vLayout)
return groupBox
def createSecondExclusiveGroup(self):
groupBox = QGroupBox('E&xclusive Radio Buttons', self)
groupBox.setCheckable(True)
groupBox.setChecked(True)
radio1 = QRadioButton('Rad&io button1', self)
radio1.setChecked(True)
radio2 = QRadioButton('Radi&o button2', self)
radio3 = QRadioButton('Radio &button3', self)
chkBox = QCheckBox('Ind&ependent checkbox', self)
vLayout = QVBoxLayout(groupBox)
vLayout.addWidget(radio1)
vLayout.addWidget(radio2)
vLayout.addWidget(radio3)
vLayout.addWidget(chkBox)
vLayout.addStretch(1)
groupBox.setLayout(vLayout)
return groupBox
def createNonExclusiveGroup(self):
groupBox = QGroupBox('No-Exclusive Checkboxes', self)
groupBox.setFlat(True)
chBox1 = QCheckBox('&Checkbox 1')
chBox2 = QCheckBox('C&heckbox 2')
chBox2.setChecked(True)
tristateBox = QCheckBox('Tri-&state buttton')
tristateBox.setTristate(True)
tristateBox.setCheckState(Qt.PartiallyChecked)
vLayout = QVBoxLayout(groupBox)
vLayout.addWidget(chBox1)
vLayout.addWidget(chBox2)
vLayout.addWidget(tristateBox)
vLayout.addStretch(1)
groupBox.setLayout(vLayout)
return groupBox
if __name__ == '__main__':
app = QApplication(sys.argv)
window = DemoScrollArea()
window.show()
sys.exit(app.exec())