QTableWidget имеет несколько строк для одного индекса? - PullRequest
0 голосов
/ 20 апреля 2020

У меня есть QTableWidget, заполненный QLineEdits и QComboBoxes. У меня есть кнопка для добавления дополнительной строки внизу, но я работаю над кнопкой, чтобы клонировать строку, и это вызывает у меня некоторые странные проблемы. Лучший способ, которым я могу описать это, состоит в том, что есть несколько строк для одного индекса строки. Так, например, после того, как я несколько раз клонировал строку, если я выбрал поле, скажем (3, 1) и вызвал currentRow(), он возвращает 2. Я понятия не имею, почему это происходит, и для меня это не имеет смысла. Мой код довольно длинный и грязный, поэтому я сократил его, но основы должны быть здесь.

def OnClone(self):
    currentRow = self.tableModel.currentRow() + 1

    #self.tags is an array of all the QLineEdits in the tags column
    self.tags.insert(currentRow, QLineEdit())

    #self.cardTypes is an array of all the QComboBoxes in the cardTypes column
    self.cardTypes.insert(currentRow, QComboBox())
    self.cardTypes[currentRow].addItem("Select a card")

    #self.fieldsSource is an array of all the QComboBoxes in the fieldsSource column
    self.fieldsSource.insert(currentRow, QComboBox())
    self.fieldsSource[currentRow].addItem("Select a card first")

    #self.fieldsDestination is an array of all the QComboBoxes in the fieldsDestination column
    self.fieldsDestination.insert(currentRow, QComboBox())
    self.fieldsDestination[currentRow].addItem("Select a card first")

    #self.dictionaries in an array of all the QComboBoxes in the dictionaries column
    self.dictionaries.insert(currentRow, QComboBox())
    self.dictionaries[currentRow].addItem("Select a dictionary")

    self.tableModel.insertRow(currentRow)
    self.tableModel.setCellWidget(currentRow, 0, self.tags[currentRow])
    self.tableModel.setCellWidget(currentRow, 1, self.cardTypes[currentRow])
    self.tableModel.setCellWidget(currentRow, 2, self.fieldsSource[currentRow])
    self.tableModel.setCellWidget(currentRow, 3, self.fieldsDestination[currentRow])
    self.tableModel.setCellWidget(currentRow, 4, self.dictionaries[currentRow])

    self.tags[currentRow].setText(self.tags[currentRow - 1].text())
    self.cardTypes[currentRow].setCurrentIndex(self.cardTypes[currentRow - 1].currentIndex())
    self.fieldsSource[currentRow].setCurrentIndex(self.fieldsSource[currentRow - 1].currentIndex())
    self.fieldsDestination[currentRow].setCurrentIndex(self.fieldsDestination[currentRow - 1].currentIndex())
    self.dictionaries[currentRow].setCurrentIndex(self.dictionaries[currentRow - 1].currentIndex())

    self.tableModel.setCurrentCell(currentRow + 1, self.tableModel.currentColumn())

Это должен быть весь соответствующий код, но если вам нужно больше, не стесняйтесь спрашивать.

Моя лучшая догадка, потому что я добавляю две строки к текущей строке, но это все, что у меня есть. Я потратил на это больше времени, чем хотел бы признаться, я очень признателен за любую помощь!

...