如何将 QListWidget setText 位置更改为顶部?

How to change QListWidget setText position to top?

我正在尝试更改不同位置的 QListWidget 文本标题,从图片的右侧到顶部:

现在看起来像:

我想要这样的东西:

我的代码:

video_thumbnail.setFlow(QListView.LeftToRight)

for i in range(30):
    it = QListWidgetItem()
    it.setIcon(QIcon('test3.png'))
    it.setText(str(i))
    video_thumbnail.addItem(it)
video_thumbnail.setIconSize(QSize(128, 128))

您可以使用委托来修改图标和文本的位置:

import sys

from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (
    QApplication,
    QListView,
    QListWidget,
    QListWidgetItem,
    QStyledItemDelegate,
    QStyleOptionViewItem,
)


class StyledItemDelegate(QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super().initStyleOption(option, index)
        option.displayAlignment = Qt.AlignCenter
        option.decorationPosition = QStyleOptionViewItem.Bottom


def main():
    app = QApplication(sys.argv)

    video_thumbnail = QListWidget(flow=QListView.LeftToRight)
    video_thumbnail.setIconSize(QSize(128, 128))
    delegate = StyledItemDelegate(video_thumbnail)
    video_thumbnail.setItemDelegate(delegate)

    for i in range(30):
        it = QListWidgetItem()
        it.setIcon(QIcon("test3.png"))
        it.setText(str(i))
        video_thumbnail.addItem(it)
    video_thumbnail.resize(400, 100)
    video_thumbnail.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()