简体   繁体   English

PyQt:在QListWidget中更新自定义列表小部件项

[英]PyQt : Updating Custom List Widget Item in QListWidget

I was able to add custom QListWidgetItem in QListWidget using the following code - 我能够使用以下代码在QListWidget中添加自定义QListWidgetItem -

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 来自规格: http//pyqt.sourceforge.net/Docs/PyQt4/qlistwidget.html#setItemWidget

QListWidget.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. 如果要显示自定义动态内容或实现自定义编辑器窗口小部件,请改用QListView和子类QItemDelegate。

So if you want to work with dynamic components it's necessary to use QListView and subclass QItemDelegate instead. 因此,如果您想使用动态组件,则需要使用QListView和子类QItemDelegate。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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