3
votes

I'm currently working on a list which I'd like to have set up in a grid style and was curious how I could go about that. I don't believe a table will work because I'd like to have it formatted like this:

option  option  option  option

option  option  option  option

option  option  option  option

option  option  option  option

~4 options per row and no further information split into columns where each row represents a single ListWidgetItem.

3

3 Answers

9
votes

You're looking for something like this:

QListWidget *listWidget = new QListWidget;

//Lays out horizontally instead of vertically
listWidget->setFlow(QListView::LeftToRight);

//Dynamically adjust contents
listWidget->setResizeMode(QListView::Adjust);

//This is an arbitrary value, but it forces the layout into a grid
listWidget->setGridSize(QSize(64, 64));

//As an alternative to using setGridSize(), set a fixed spacing in the layout:
listWidget->setSpacing(someInt);

//And the most important part:
listWidget->setViewMode(QListView::IconMode);

Play with these properties in Qt Designer. Start by setting QListView::IconMode and going from there until you get the behavior you desire.

1
votes

Depends, if you have a static number of items, you can use a QGridLayout.

If you have a QAbstractListModel, you can implement your own view, by implementing QAbstractItemView and QAbstractItemDelegate, which is what QTableView does.

0
votes

You probably want to use a QTableWidget or QTreeWidget. When the items in a row are closely related to a single item (ie. each row is an item, and each column is an attribute of that item), I've found using a QTreeWidget is easier.

tree = QtGui.QTreeWidget()
tree.setHeaderLabels(['Name', 'Age', 'Weight'])
item = QtGui.QTreeWidgetItem(tree, ['John Doe', '24', '150'])
item = QtGui.QTreeWidgetItem(tree, ['Jane Doe', '21', '120'])