简体   繁体   中英

PyQt : Updating Custom List Widget Item in QListWidget

I was able to add custom QListWidgetItem in QListWidget using the following code -

for item in dl_list:            
        widget = QtWidgets.QWidget()
        card = Ui_DownloadCard()
        card.setupUi(widget)
        card.set_filename(item["title"])
        card.set_progress_bar(item["progress"])
        card.set_progress_text(item["progress"]/item["size"])
        card.set_speed(item["speed"])

        listItem = QtWidgets.QListWidgetItem(self.download_list)
        listItem.setSizeHint(widget.sizeHint())

        self.myListWidget.addItem(listItem)
        self.myListWidget.setItemWidget(listItem, widget)

Now I wish to update each item with new speed & progress. I tried the following code -

self.myListWidget.item(0).set_speed("300 KB/s")

But it gives error saying

AttributeError: 'QListWidgetItem' object has no attribute 'set_speed'

So what is the correct way to update the item?

listItem = QtWidgets.QListWidgetItem(self.download_list)

您需要使用自定义项目:

listItem = YourCustomListWidgetItem(self.download_list)

From specs: http://pyqt.sourceforge.net/Docs/PyQt4/qlistwidget.html#setItemWidget

QListWidget.setItemWidget

This function should only be used to display static content in the place of a list widget item. If you want to display custom dynamic content or implement a custom editor widget, use QListView and subclass QItemDelegate instead.

So if you want to work with dynamic components it's necessary to use QListView and subclass QItemDelegate instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM