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?

时间:2019-04-08 14:06:08

标签: python qlistwidget pyside2

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

1 个答案:

答案 0 :(得分:2)

您必须将flow属性设置为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