Search code examples
pythonqlistwidgetpyside2

How can I have the icon items in my QlistWidget insert into the widget, or display, from top to bottom and not left to right?


I am using PySide. I have a QlistWidget that I am populating with icon items. The icons display left to right by default and I would like them to display top to bottom, or have a vertical layout, not a horizontal one. How can I achieve this?

from this:enter image description here

to this:

enter image description here


Solution

  • You have to set the flow property to QListView::TopToBottom:

    from PySide2 import QtCore, QtGui, QtWidgets
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = QtWidgets.QListWidget()
        w.setViewMode(QtWidgets.QListView.IconMode)
        w.setIconSize(QtCore.QSize(128, 128))
        w.setResizeMode(QtWidgets.QListView.Adjust)
        w.setFlow(QtWidgets.QListView.TopToBottom)
        for path in ("icon1.png", "icon2.png"):
            it = QtWidgets.QListWidgetItem()
            it.setIcon(QtGui.QIcon(path))
            w.addItem(it)
        w.resize(640, 480)
        w.show()
        sys.exit(app.exec_())
    

    enter image description here