Hensalbert
Hensalbert

Reputation: 21

PyQt5 - Getting information from a QcomboBox on TableWidget

I'm looking for some piece of code that enables me to get the information of a combobox that I've put it on a TableView created by QtDesigner.

For lack of knowledge I'm not using any classes or Delegates. When I was using TableWidget the line TableWidget.cellWidget(indexofthecurrentrow, 4).currentIndex() == 0 returned me the current status of the row allowing me to update the database, but that line doesn't work on TableView I'm assuming because of the model or absence of a Delegate.

Relevant Code Below:

for row in range(len(data_from_sqlite)):
      comboBox = QComboBox()
      comboBox.addItems('opt1', 'opt2')
      index_combo = tableview.model().index(row, 5) 
      tableview.setIndexWidget(index_combo, comboBox)

I just don't know how to retrieve this QComboBox state through other function connected in a button. I've tried both

tableview.model().index(0,4).itemData().currentIndex() #crashes

and

tableview.model().index(0,4).data() #returns None

Thanks in advance for any help.

Upvotes: 1

Views: 1181

Answers (2)

Hensalbert
Hensalbert

Reputation: 21

I found a solution to a my problem and as pointed by musicamante the solution involves using classes witch is very basic and was very difficult for me.

The loop that implements the combobox in the tableview is:

for row in range(len(data)):
    combo = combomaker(self)
    index_combo = self.tableview.model().index(row, 5)
    self.tableview.setIndexWidget(index_combo, combo)

being the combo maker as it follows

class combo(QComboBox):
    def __init__(self, parent):
        super().__init__(parent)
        self.addItems(['a', 'b'])
        self.currentIndexChanged.connect(self.getComboValue)

    def getComboValue(self):
        print(self.currentText())
        # return self.currentText()

I took the code from Jie Jenn https://www.youtube.com/watch?v=KMJTJNUzo4E

Hope this help some clueless programmer in the future.

Upvotes: 1

musicamante
musicamante

Reputation: 48499

Using cell widgets makes absolutely no interaction with the table's model and its data.

If you need to access a specific cell widget, use the getter function opposite to setIndexWidget(), indexWidget():

combo = tableview.indexWidget(tableview.model().index(0,4))
combo_index = combo.currentIndex()

Upvotes: 0

Related Questions