Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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.
    0_1557170701266_5c2a2ee8-f709-4ab0-aa8d-e5b59450dfc3-image.png


  • 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 https://stackoverflow.com/questions/9485339/design-pattern-qt-model-view-and-multiple-threads.

    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")
            else:
                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)
            self.icon_size=button.size()
    
            # 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())
    
            button.setIcon(QtGui.QIcon(pixmap))
            button.setIconSize(button.size())
            button.setFlat(True)
            label = QLabel(file["n_file"])
            layout = QHBoxLayout()
            layout.addWidget(button, 0)
            layout.addWidget(label, 0)
            layout.setContentsMargins(0, 0, 0, 0)
            self.setLayout(layout)
    
    

    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