Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Update QListWidget item
Qt 6.12 LTS is out! See what's new in the release blog

Update QListWidget item

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 3.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zaher
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • Z Offline
        Z Offline
        Zaher
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          On how to use what ?

          Did you already took a look at:

          • Model/View Programming
          • Item views examples
          • Model View Tutorial

          ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • Z Offline
            Z Offline
            Zaher
            wrote on last edited by
            #5

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

            Gojir4G 1 Reply Last reply
            0
            • Z Zaher

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

              Gojir4G Offline
              Gojir4G Offline
              Gojir4
              wrote on last edited by
              #6

              @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.

              Z 1 Reply Last reply
              2
              • Gojir4G Gojir4

                @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.

                Z Offline
                Z Offline
                Zaher
                wrote on last edited by
                #7

                @Gojir4 Thanks.

                1 Reply Last reply
                0
                • Z Offline
                  Z Offline
                  Zaher
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    What do you mean by edit ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • Z Offline
                      Z Offline
                      Zaher
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

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

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt
                        • Unsolved