Search code examples
pythonpyqtpyqt5qlistwidgetqlistwidgetitem

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


Here is an elastic layout of a single item i'm trying to design :

my desired layout

Please find below the code i'm using to display that custom QListWidgetItem in a QlistWidget. But the layout does not look as expected :

  • custom widget does not fit the width of the list, I don't want a horizontal scroll bar
  • even with setWordWrap set to True, long description does not display the whole text and is cut in its height

The code :

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_())

Solution

  • If using the QListWidget is mandatory, then you have to provide only the hint for the required orientation.

    The returned sizeHint of a widget is always a size that recommended, aka is the optimal size to ensure that all widget have the correct size to show their contents (which is usually bigger than the minimum size).

    The sizeHint of an item view, instead, is the default size that the item will use, and since QListWidget has no horizontal header, there's no direct way to "stretch" the contents to a size smaller than the hint.

    The solution is to invalidate the orientation of the size hint for which you're not interested, by setting its value to -1:

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