How to make QWidget contents scrollable in PyQt?
pyqt, python, qt4
Solution
Try `scroll.setWidgetResizable(True)` like in here:
def initWidget(self, items):
listBox = QVBoxLayout(self)
self.setLayout(listBox)
scroll = QScrollArea(self)
listBox.addWidget(scroll)
scroll.setWidgetResizable(True)
scrollContent = QWidget(scroll)
scrollLayout = QVBoxLayout(scrollContent)
scrollContent.setLayout(scrollLayout)
for item in items:
scrollLayout.addWidget(item)
scroll.setWidget(scrollContent)
Problem
First, I started using PyQt few hours ago. So far so good - im writing rss client to familiarize myself with PyQt I got `QApplication`, `QMainWindow` and two custom widgets. First custom widget is: ``` class RssItem(QWidget): __pyqtSignals__ = ("articleViewed(bool)", "articleOpened(bool)", "articleMarkedGood(bool)") def __init__(self, title, date, parent = None): super(RssItem, self).__init__(parent) self.initWidget(title, date) def initWidget(self, title, date): title = QLabel(title) date = QLabel(date) titleBox = QHBoxLayout() titleBox.addWidget(title) titleBox.addWidget(date) self.setLayout(titleBox) ``` That displays (for now) title and date in single row Second one accepts array of `RssItem` widgets and display them in vertical list: ``` class ItemsList(QWidget): def __init__(self, items, parent=None): super(ItemsList, self).__init__(parent) self.initWidget(items) def initWidget(self, items): listBox = QVBoxLayout(self) for item in items: listBox.addWidget(item) listBox.addStretch(1) self.setLayout(listBox) ``` How do I make this list scrollable? Keep in mid I'm planing to have multiple `ItemList`'s in one window each should have it's own scrollbar. Main app function as for now is only for testing these 2 widgets: ``` class MainApp(Qt.QApplication): def __init__(self, args): super(MainApp, self).__init__(args) self.addWidgets() self.exec_() def addWidgets(self): self.window = MainWindow() class MainWindow(QtGui.QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.initUI() def initUI(self): self.statusBar().showMessage("ok") self.resize(640, 480) self.setWindowTitle("Smart Rss") items=[] for x in range(0, 200): items.append(RssItem("Title no %s" % x, "2000-1-%s" %x)) self.setCentralWidget(ItemsList(items)) self.show() ``` EDIT:Getting closer, changed `ItemList.initWidget` to ``` def initWidget(self, items): scroll= QScrollArea(self) wrap = QWidget(self) listBox = QVBoxLayout(self) for item in items: listBox.addWidget(item) listBox.addStretch(1) wrap.setLayout(listBox) scroll.setWidget(wrap) ``` But now I cant figure out how to make `QScrollArea` fill all available space and auto resize when it's changed.