如何使 QListWidgetItem 自动调整其内容,并适合其 QListWidget

How make QListWidgetItem autosize to its content, and fit into its QListWidget

这是我正在尝试设计的单个项目的弹性布局:

请在下面找到我用来在 QlistWidget 中显示自定义 QListWidgetItem 的代码。 但是布局看起来不像预期的那样:

代码:

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.resize(400, 500)
        self.centralwidget = QWidget(MainWindow)
        self.gridLayout = QGridLayout(self.centralwidget)
        self.listWidget = QListWidget(self.centralwidget)
        self.gridLayout.addWidget(self.listWidget, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)

class customWidget(QWidget):
    def __init__ (self, name, date, desc, parent = None):
        super(customWidget, self).__init__(parent)
        
        # feed labels
        self.labelName = QLabel(name)
        self.labelName.setFrameShape(QFrame.Box)
        self.labelName.setStyleSheet('''font: bold;''')

        self.labelDate = QLabel(date)
        self.labelDate.setFrameShape(QFrame.Box)

        self.labelDesc = QLabel(desc)
        self.labelDesc.setFrameShape(QFrame.Box)
        self.labelDesc.setWordWrap(True)

        # layout
        self.Hlayout = QHBoxLayout()
        self.Hlayout.addWidget(self.labelName)
        self.Hlayout.addStretch()
        self.Hlayout.addWidget(self.labelDate)

        self.Vlayout = QVBoxLayout()
        self.Vlayout.addLayout(self.Hlayout)
        self.Vlayout.addWidget(self.labelDesc)
        
        self.setLayout(self.Vlayout)

def feed_list():
    myArray = [
        ("paul meneck", "21/09/1969", "blond whith red hat"),
        ("boby la pointe", "16/08/1972", "brown shoes"),
        ("julius con-meraz de la fuego", "07/12/1980","this is a very long description"
            "with more and more bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla "
            "bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla "
            "bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla "
            "and finaly the END OF THAT LONG DESCRIPTION"),
        ("roberto umportes", "14/01/1957", "another description for that roberto")
    ]
    for var in myArray:
        item = QListWidgetItem()
        widget = customWidget(var[0], var[1], var[2])
        item.setSizeHint(widget.sizeHint())
        ui.listWidget.addItem(item)
        ui.listWidget.setItemWidget(item, widget)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    feed_list()
    sys.exit(app.exec_())

如果必须使用 QListWidget,则您只需提供所需方向的提示。

小部件返回的 sizeHint 始终是推荐 的大小,也就是最佳 大小,以确保所有小部件都具有显示其内容的正确大小(通常大于 最小 大小)。

项视图的 sizeHint 相反,是项将使用的默认大小,并且由于 QListWidget 没有水平 header,因此没有直接的方法将内容“拉伸”到更小的尺寸比提示。

解决方案是通过将其值设置为 -1 来使您不感兴趣的尺寸提示的方向无效:

    widget = customWidget(var[0], var[1], var[2])
    hint = widget.sizeHint()
    hint.setWidth(-1)
    item.setSizeHint(hint)