The present invent
Q: What is the us
Oliver, Wisconsin
/* * Licensed to
Q: Python: How to
Tales from The Loo
Inhibitory effect
If so, would you e
Square Enix has an
Q: Square root (s
Q: How can I change how many items are on a line in a QTreeView? Using PyQt5 I have the code below: self.model = QtGui.QStandardItemModel(self) ... self.ui.listview.setModel(self.model) ... class MyModel(QtGui.QStandardItemModel): def __init__(self): QtGui.QStandardItemModel.__init__(self) self.items = [] self.headings = ['A', 'B', 'C', 'D'] for item in self.headings: self.items.append(QtGui.QStandardItem(item)) self.appendRow(self.items[0]) The items are added as below: items = [QtGui.QStandardItem(item) for item in self.headings] This results in the following UI layout: How can I control the number of items on each line? The desired layout should be: [A] [B] [C] [D] How can I achieve this? A: One simple solution would be to subclass QStandardItemModel and override QStandardItemModel.rowCount to return the correct number. class LimitedItemModel(QtGui.QStandardItemModel): def __init__(self, parent=None): QtGui.QStandardItemModel.__init__(self, parent) self.items = [] self.headings = ['A', 'B', 'C', 'D'] for item in self.headings: self.items.append(QtGui.QStandardItem(item)) self.appendRow(self.items[0]) def rowCount(self, parent=None, *args, **kwargs): return min(len(self.items), parent.rowCount() + 1) Then use the LimitedItemModel as the model of the QTreeView: class MainWindow(QtWidgets.QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.listview = QtWidgets.QListView(self) self.listview.setModel(LimitedItemModel()) ... Result: Edit: The above solution would make each item expand to fill the width of the column, since its width would be that of the longest item in its column, which would be very wide. To make each item be only as wide as its text you can set setStretchFactor(0) on the QStandardItem widgets: class LimitedItemModel(QtGui.QStandardItemModel): def __init__(self, parent=None): QtGui.QStandardItemModel.__init__(self, parent) self.items = [] self.headings = ['A', 'B', 'C', 'D'] for item in self.headings: self.items.append(QtGui.QStandardItem(item)) self.appendRow(self.items[0]) def rowCount(self, parent=None, *args, **kwargs): return min(len(