PyQt4 - Remove widget item from QListWidget

I have a QListWidget and I need to remove some elements.

From what I explored, this is generally an unpleasant thing.

I read a ton of solutions, but none of them apply to my specific scenario.
At the moment, I only have the actual Item widgets; not their values ​​or index.

This is because I get the elements (need to be removed) through .selectedItems() .

Here is the code:

 ItemSelect = list(self.ListDialog.ContentList.selectedItems()) for x in range (0, len(ItemSelect)): print self.ListDialog.ContentList.removeItemWidget(ItemSelect[x]) 

It does nothing. This does not cause an error, but the selected items are not deleted.
The methods I saw to remove items require either an index or an item name, none of which I have. I only have actual widgets.

How to remove them?

Did I miss something?

I use:

Python 2.7.1
PyQt4 IDLE 1.8
Windows 7

+6
source share
3 answers

takeItem () should work:

 for SelectedItem in self.ListDialog.ContentList.selectedItems(): self.ListDialog.ContentList.takeItem(self.ListDialog.ContentList.row(SelectedItem)) 
+12
source

Removing an item from a ListWidget:

 item = self.listWidget.takeItem(self.listWidget.currentRow()) item = None 
+5
source

Oddly enough, there is no direct way to remove elements from a QListWidget ... Try the following:

 listWidget = self.ListDialog.ContentList model = listWidget.model() for selectedItem in listWidget.selectedItems(): qIndex = listWidget.indexFromItem(selectedItem) print 'removing : %s' %model.data(qIndex).toString() model.removeRow(qIndex.row()) 
+2
source

Source: https://habr.com/ru/post/897673/


All Articles