Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I edit a QListWidget item without removing it to edit and adding back?

I would like to edit listWidget items that are selected via a button programmatically. I am not entirely sure if there is a way to edit selected items without having to remove the original items and add the new edit back in.

I saw this... but I am not sure this is what I need, as I can't pass in a new value:

selItems = listWidget.selectedItems()

for item in selItems:
    listWidget.editItem(item, "test")

TypeError: QListWidget.editItem(QListWidgetItem): too many arguments
like image 909
Zak44 Avatar asked Oct 27 '25 16:10

Zak44


1 Answers

The editItem method is used when you want the user to edit the item. If the item is editable, by default, it will create a QLineEdit widget in the cell for the user to edit the text, unless you've created a QItemDelegate to create a different widget for editing.

To change the text of an item, just use setText(). You can use text() to get the current text of the item.

sel_items = listWidget.selectedItems()

for item in sel_items:
    item.setText(item.text() + ' plus more text')
like image 91
Brendan Abel Avatar answered Oct 29 '25 07:10

Brendan Abel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!