Basic Implementation of Scroll Area in PyQt4
-
PyQt4 / Python 3 / Windows 7
Hello -- I am fairly new to PyQt and I'm having a great deal of trouble figuring how out to implement a basic scroll area. Attached is some simple sample code - how can I set this window to scroll rather than extend the window below the screen edge? Thank you
@import sys
from PyQt4 import QtGui
from random import choiceclass ThisWindow(QtGui.QWidget):
def init(self):
super(ThisWindow, self).init()
self.initUI()def initUI(self): vbox = QtGui.QVBoxLayout() itemlist = GenerateList() for item in itemlist: newlabel = QtGui.QLabel(item) newcheckbox = QtGui.QCheckBox() newcombobox = QtGui.QComboBox() new_row_box = QtGui.QHBoxLayout() new_row_box.addWidget(newlabel) new_row_box.addWidget(newcheckbox) new_row_box.addWidget(newcombobox) new_row_box.addStretch(1) vbox.addLayout(new_row_box) self.setGeometry(100,100,800,600) self.setLayout(vbox) self.setWindowTitle('How do I make this window scroll if it gets beyond a certain height (e.g. the edge of the screen?)') self.show()
def GenerateList():
itemlist = []
top = choice(range(50,150))
for x in range(0,top):
item = "Label # {0}".format(x)
itemlist.append(item)
return itemlistdef main():
app = QtGui.QApplication(sys.argv)
win = ThisWindow()
sys.exit(app.exec_())if name == 'main':
main()@ -
The best way to do this is using QScrollArea as demonstrated below:
@
from PyQt4.QtGui import *
from random import choiceclass ThisWidget(QWidget):
def init(self, parent=None):
QWidget.init(self, parent)l=QVBoxLayout(self) l.setContentsMargins(0,0,0,0) l.setSpacing(0) s=QScrollArea() l.addWidget(s) w=QWidget(self) vbox=QVBoxLayout(w) for x in range(0, choice(range(50,150))): _l=QHBoxLayout() _l.addWidget(QLabel("Label # %d" % x, self)) _l.addWidget(QCheckBox(self)) _l.addWidget(QComboBox(self)) _l.addStretch(1) vbox.addLayout(_l) s.setWidget(w) self.setGeometry(100, 100, 800, 600) self.setWindowTitle("This is how you make the window scroll if it gets beyond a certain height")
if name=="main":
from sys import argv, exit
a=QApplication(argv)
win=ThisWidget()
win.show()
win.raise_()
exit(a.exec_())
@Hope this helps.