Obj3ctiv3_C_88 Obj3ctiv3_C_88 - 5 months ago 61
Python Question

How to create grid style QListWidget?

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.

Answer

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.