Issue
I have a scroll area containing a list of checkboxes. I would like this scroll area to resize to the size of the application. Can't figure out how to do this. Any suggestions would be appreciated. I have come this problem in other posts but have not found a solution, or at least a solution I understood.
from PyQt4 import QtGui, QtCore
class Screen3(QtGui.QWidget): # Screen to display data
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
# layout for check box container widget
chkBoxLayout = QtGui.QVBoxLayout(self)
# create list of checkboxes
self.checkboxes = []
numofCheckboxes = 30
for x in range(numofCheckboxes):
self.checkboxes.append(QtGui.QCheckBox('test'))
# add checkboxes to chkBoxLayout
for i, chkbox in enumerate(self.checkboxes):
chkbox.setChecked(True)
chkBoxLayout.addWidget(chkbox)
chkBoxLayout.addStretch(1)
chkBoxLayout.setMargin(0);
chkBoxLayout.setContentsMargins(0,0,0,0)
chkBoxLayout.setSpacing(0)
# checkbox container widget
widget = QtGui.QWidget()
widget.setStyleSheet(""".QWidget {background-color: rgb(255, 255, 255);}""")
widget.setLayout(chkBoxLayout)
# checkbox scroll area, gives scrollable view on widget
scroll = QtGui.QScrollArea()
scroll.setMinimumWidth(120)
scroll.setMinimumHeight(200) # would be better if resizable
scroll.setWidgetResizable(True)
scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
scroll.setWidget(widget)
self.samplePanel = QtGui.QVBoxLayout()
self.samplePanel.addWidget(scroll)
self.samplePanel.addStretch(1)
# horizontal layout
hbox = QtGui.QHBoxLayout()
hbox.addLayout(self.samplePanel)
hbox.addStretch(1)
self.setLayout(hbox)
self.resize(100, 500)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Screen = Screen3()
Screen.show()
sys.exit(app.exec_())
Solution
You have to add the QScrollArea
to the main layout, you do not need to create other layouts.
class Screen3(QtGui.QWidget): # Screen to display data
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
# layout for check box container widget
chkBoxLayout = QtGui.QVBoxLayout()
# create list of checkboxes
self.checkboxes = []
numofCheckboxes = 30
for x in range(numofCheckboxes):
self.checkboxes.append(QtGui.QCheckBox('test'))
# add checkboxes to chkBoxLayout
for i, chkbox in enumerate(self.checkboxes):
chkbox.setChecked(True)
chkBoxLayout.addWidget(chkbox)
chkBoxLayout.addStretch(1)
chkBoxLayout.setMargin(0);
chkBoxLayout.setContentsMargins(0,0,0,0)
chkBoxLayout.setSpacing(0)
# checkbox container widget
widget = QtGui.QWidget()
widget.setStyleSheet(""".QWidget {background-color: rgb(255, 255, 255);}""")
widget.setLayout(chkBoxLayout)
# checkbox scroll area, gives scrollable view on widget
scroll = QtGui.QScrollArea()
scroll.setMinimumWidth(120)
scroll.setMinimumHeight(200) # would be better if resizable
scroll.setWidgetResizable(True)
scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
scroll.setWidget(widget)
lay = QtGui.QVBoxLayout(self)
lay.addWidget(scroll)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.