delete items from 3 different lists - python

I need some help with deleting some items from some lists at the same time, when a button is clicked.
This is the code:
class Window(QMainWindow):
list_1 = [] #The items are strings
list_2 = [] #The items are strings
def __init__(self):
#A lot of stuff in here
def fillLists(self):
#I fill the lists list_1 and list_2 with this method
def callAnotherClass(self):
self.AnotherClass().exec_() #I do this to open a QDialog in a new window
class AnotherClass(QDialog):
def __init__(self):
QDialog.__init__(self)
self.listWidget = QListWidget()
def fillListWidget(self):
#I fill self.listWidget in here
def deleteItems(self):
item_index = self.listWidget.currentRow()
item_selected = self.listWidget.currentItem().text()
for i in Window.list_2:
if i == item_selected:
?????????? #Here is where i get confussed
When i open the QDialog with a combination of keys, i see in the QListWidget some items. In the deleteItems method, i get the index and the text from the item that i selected in the QListWidget. That works fine.
What i need to do is to delete the item from the list_1, list_2, and the QListWidget when i press a button (that i have already created).
How can i do this? Hope you can help me.

Python lists have a "remove" object that perform that action directly:
Window.list_2.remove(item_selected)
(with no need for your for loop)
If you ever need to perform more complex operations on the list items, you can retrieve an item's index with the index method instead:
position = Window.list_2.index(item_selected)
Window.list_2[position] += "(selected)"
And in some ocasions you will want to do a for loop getting to the actual index, as well as the content at that index of a list or other sequence. In that case, use the builtin enumerate.
Using the enumerate pattern, (if removedid not exist) would look like:
for index, content in enumerate(Window.list_2):
if content == item_selected:
del Window.list_2[index]
# must break out of the for loop,
# as the original list now has changed:
break

if you have the value, then just find its index in each list and then delete it. Something like:
item_selected = self.listWidget.currentItem().text()
i = Window.list_2.index(item_selected)
if i >= 0:
del Window.list_2[i]
You can also use directly Widow.list_x.remove(value) but it can throw an exception if the value does not exist.

Related

How to move multiple lines at once up or down in PyQt List Widget?

I am using the following code to move a single item up in the PyQt6 list widget
def move_item_up_in_list_box(self):
row = self.listWidget.currentRow()
text = self.listWidget.currentItem().text()
self.listWidget.insertItem(row-1, text)
self.listWidget.takeItem(row+1)
self.listWidget.setCurrentRow(row-1)
But I couldn't find an option to get the index positions when multiple lines are selected, though
'self.listWidget.selectedItems()' returns the texts in the selected items, I couldn't figure out how to move multiple lines up or down.
Just cycle through the selectedItems() and use row() to get the row of each one.
for item in self.listWidget.selectedItems():
row = self.listWidget.row(item)
# ...
Consider that the selection model usually keeps the order in which items have been selected, so you should always reorder the items before moving them, and remember that if you move items down, they should be moved in reverse order.
def move_items(self, down=False):
items = []
for item in self.listWidget.selectedItems():
items.append((self.listWidget.row(item), item))
items.sort(reverse=down)
delta = 1 if down else -1
for row, item in items:
self.listWidget.takeItem(row)
self.listWidget.insertItem(row + delta, item)

Remove item from QListView\QListWidget by string name

In PyQt4, How do I delete an item from the QListView\QListWidget by a name string?
def deleteItem(itemName):
item = dialog.listWidget.indexFromItem(itemName)
dialog.listWidget.takeItem(item)
This is what I've got so far, and obviously feeding string to indexFromItem is not working...
First, look for the item in listWidget using findItems:
items_list = dialog.listWidget.findItems(itemName,Qt.MatchExactly)
This will return a list of matched QListWidgetItem with itemName (which should be a list of one item if there is only one item with itemName)
Second, call dialog.listWidget.row() to find row number of your found item(s).
Finally, detele that/those item(s) using dialog.listWidget.takeItem()
So at the end you function would look like this:
def deleteItem(itemName):
items_list = dialog.listWidget.findItems(itemName,QtCore.Qt.MatchExactly)
for item in items_list:
r = dialog.listWidget.row(item)
dialog.listWidget.takeItem(r)
Again, you have to make sure there are no items with same names otherwise they would be deleted all together.

How to display a QListWidget Item

I am running the below code:
self.myList = QListWidget()
for i in range(3):
self.Item = QListWidgetItem()
self.name = 'A'+'%04d'% i
self.Item.setText(self.name)
self.myList.addItem(self.Item)
print self.myList.selectedItems()
it prints an empty list:
[]
Please suggest where I am missing.
According to QT docs (i've used c++ ones, but it's ok for python bridge as well),
QList<QListWidgetItem *> QListWidget::selectedItems () const
Returns a list of all selected items in the list widget.
This means, that print self.myList.selectedItems() will output all the list of items user was selected, not all the items are in widget.
You can try to use count() to get number of items and item(number) to get one.

QTreewidget only displays first letter of item name

Im learning how to use QTreeWidget and Im stuck adding new items to it. The QTreewidget itself is created with qtdesigner, so my idea was just to add items. eg:
tw = self.ui.treeWidget
item = QtGui.QTreeWidgetItem("TEST")
tw.addTopLevelItem(item)
But in the treewidget only appears the first letter of "TEST". Doesnt matter what I type, it always only displays the first letter and I have no idea why...
QTreeWidgetItem constructor expects a list of strings. Try this:
tw = self.ui.treeWidget
item = QtGui.QTreeWidgetItem(["TEST"])
tw.addTopLevelItem(item)
The QtGui.QTreeWidgetItem is expecting a list for different columns. You can simply wrap your text in a list
item = QtGui.QTreeWidgetItem(["TEST"])
or you can set the text for a specific column.
item = QtGui.QTreeWidgetItem()
item.setText(0, "TEST")

Switch items between two QListWidgets in python

i am currently designing a QT-gui in Python and i want to allow the user to switch QListWidgetItems between two QListWidgets. Multiple selection (CTRL) is allowed and switching is done by two control buttons.
In the QT4-Designer the lists looks like this
So if the user selects for example two items from the left list and clicks on the '>' Button the items have to be added to the right list and consequently deleted from the left list.
My current triggered Button-Events look like this:
def switchR( self ):
itemlistSel = self.list_left.selectedItems()
for item in itemlistSel:
self.list_right.addItem( item )
self.list_left.removeItemWidget( item )
But nothing happens? Someone got a quick solution?
The removeItemWidget() method doesn't quite do what you're expecting it to do (see docs). Use takeItem(), addItem() and row() instead:
def switch(self):
items = self.left.selectedItems()
for item in items:
n = self.left.row(item) # get the index/row of the item
i = self.left.takeItem(n) # pop
self.right.addItem(i) # add to right QListWidget

Categories

Resources