Search code examples
pythonqtpyqtqt4pyside

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.


Solution

  • You're looking for something like this (explanation is inline):

    QListWidget *listWidget = new QListWidget;
    
    //Dynamically adjust contents to viewport size.
    listWidget->setResizeMode(QListView::Adjust);
    
    //Forces the layout into a grid (64x64 is an arbitrary value).
    listWidget->setGridSize(QSize(64, 64));
    
    //As an alternative to using setGridSize(), set a fixed spacing between items in the layout:
    listWidget->setSpacing(someInt);
    
    //This sets the Flow to LeftToRight, which is more natural in a grid.
    listWidget->setFlow(QListView::LeftToRight);
    

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