Important: Please read the Qt Code of Conduct -

Update QListWidget item

  • Hi,

    I want to create a list of items where each item has an icon and label, but the icon could change depends on condition. I used QListWidget with CustomeItem. For updating the item, firstly I delete it then create a new one and insert it at the same index. I am wondering if there a widget like QListWidget but I can update the item without deleting.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why are you deleting your CustomItem ? If you make it properly updatable you can avoid that.

  • Thanks @SGaist for the reply, but is there a documentation page of example I can read to know how to use it.

  • Lifetime Qt Champion

    On how to use what ?

    Did you already took a look at:


  • I am wonder if the model works on a diffrent thread or not?

  • @Zaher No, models are working on the GUI main thread. See

    But this does not avoid to handle heavy process in different thread and then update the model using signal and slots.

  • @Gojir4 Thanks.

  • Do you have any example for edit item by code in Python using PyQt?

  • Lifetime Qt Champion

    What do you mean by edit ?

  • I issue is I want to change the icon in QListItem on condition this the code of widget of the item
    In this code, I am reading from pass tuple "file" and depends on the condition I am choosing the icon

    import os
    from PyQt5 import QtGui, QtCore
    from PyQt5.QtWidgets import QWidget, QLabel, QHBoxLayout, QPushButton
    class CustomQWidget(QWidget):
        def __init__(self, file, parent=None):
            super(CustomQWidget, self).__init__(parent)
            self.file = file
            if self.file["l_file"]:
                pathname = os.path.join(parent.parent.main_script_path, "icons/correct.png")
                pathname = os.path.join(parent.parent.main_script_path, "icons/wrong.png")
            pixmap = QtGui.QPixmap(pathname)
            button = QPushButton()
            button.setStyleSheet("padding: 0px;")
            button.setFixedSize(16, 16)
            # resize pixmap
            pixmap = pixmap.scaled(button.size(), QtCore.Qt.KeepAspectRatioByExpanding, QtCore.Qt.SmoothTransformation)
            # crop pixmap - the following assumes the image aspect is always wider than the button.  if that's not the case
            # you'll need to compare your image/button aspects and crop vertically/horizontally as necessary
            self.cropOffsetX = (pixmap.width() - button.size().width()) / 2
            pixmap = pixmap.copy(self.cropOffsetX, 0, button.size().width(), button.size().height())
            label = QLabel(file["n_file"])
            layout = QHBoxLayout()
            layout.addWidget(button, 0)
            layout.addWidget(label, 0)
            layout.setContentsMargins(0, 0, 0, 0)

    So, what I understand I could QStyledItemDelegate to rerender the item when I want to change the Icon I found a close question but I couldn't finger out how to connect it to the icon and how to make it render on a certain single.
    Please, help I am desperate here, and thanks in advance.

  • Lifetime Qt Champion

    Something is not clear, do you want to use a delegate or have cell widgets ?

Log in to reply