Pablo Flores
Pablo Flores

Reputation: 717

Getting an index that is not the right one

I asked this question a few days ago:

enter link description here

And i solved the problem with your help. Now i just added a few more lines in the method, and i am getting an index error that i did not have before.

This is the code with the extra lines that i added:

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, parent):
    super(QDialog,self).__init__(parent)

    self.listWidget = QListWidget()

  def fillListWidget(self):
    #I fill self.listWidget in here

  def deleteItems(self):
    item_index = self.listWidget.currentRow()
    self.listWidget.takeItem(item_index)
    item_selected = self.listWidget.currentItem().text()
    list_2_item = Window.list_2.index(item_selected)

    for index, content in enumerate(Window.list_2):
        if content == item_selected:
            del Window.list_2[index]
            del Window.list_1[index]

            widget = self.parent().splitter.widget(index)
            widget.hide()

            break

SO, when i print the item_selected variable, the text is not the one that i selected in the ListWidget. For example, if i have 2 items, i select the item 1, the item_selected variable prints "item 2".

I really do not know where is the problem.

Hope you can help me.

Upvotes: 0

Views: 65

Answers (1)

Laurent H.
Laurent H.

Reputation: 6526

You are using takeItem() method, that acts as a pop(). It means that when you use currentItem() method just after, it returns you the next item.

Solution for you is to call currentItem() before calling takeItem().

Upvotes: 1

Related Questions