Dhurjati Riyan
Dhurjati Riyan

Reputation: 108

pyqt5 comboBox - get the associate value for selected item

enter image description here

i want to set a comboBox and show my table's column1 data as items and associated value column2 is for selected item id and want to setText in a qLabel. i am using a model to view the items in comboBox and working fine. i can get the currentText value but how to get the associated value for the items. in my case : my sql table like:

type     id
------------
DIV        2
TRANS     33
FEE       41
EXP       89

now , i can set the column1(type) values into comboBox successfully. now, if user selected value 'FEE' , than qlable should be updated as its associate id : 41. how to do that!

df=loadData()
model=PandasModel(df)

self.comboBox.setModel(model)
self.comboBox.setModelColumn(0) 

content=self.comboBox.currentText()
self.label.setText(content) # content should be ID instead of currentText

pandasMode:

class PandasModel(QtCore.QAbstractTableModel): 
    def __init__(self, df = pd.DataFrame(), parent=None): 
        QtCore.QAbstractTableModel.__init__(self, parent=parent)
        self._df = df

    def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):

        if role != QtCore.Qt.DisplayRole:
            return QtCore.QVariant()

        if orientation == QtCore.Qt.Horizontal:
            try:
                return self._df.columns.tolist()[section]
            except (IndexError, ):
                return QtCore.QVariant()
        elif orientation == QtCore.Qt.Vertical:
            try:
                return self._df.index.tolist()[section]+1
            except (IndexError, ):
                return QtCore.QVariant()

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if index.isValid():
            if role == QtCore.Qt.DisplayRole:
                return str(self._df.iloc[index.row(), index.column()])
        return None
        
    def setData(self, index, value, role):
       
      
        if not index.isValid():
            return False
        if role != QtCore.Qt.EditRole:
            return False
        row = index.row()
        if row < 0 or row >= len(self._df.values):
            return False
        column = index.column()
        if column < 0 or column >= self._df.columns.size:
            return False
        self._df.values[row][column] = value
        self.dataChanged.emit(index, index)
        return True
    
    # def rowCount(self, parent=QtCore.QModelIndex()): 
    #     return len(self._df.index)
    def rowCount(self, parent=None):
        return len(self._df.index)

    def columnCount(self, parent=QtCore.QModelIndex()): 
        return len(self._df.columns)

    def sort(self, column, order):
        colname = self._df.columns.tolist()[column]
        self.layoutAboutToBeChanged.emit()
        self._df.sort_values(colname, ascending= order == QtCore.Qt.AscendingOrder, inplace=True)
        self._df.reset_index(inplace=True, drop=True)
        self.layoutChanged.emit()

Upvotes: 0

Views: 1809

Answers (1)

eyllanesc
eyllanesc

Reputation: 244162

Assuming that the model stores the id in the second column then it has to obtain the associated index:

ID_COLUMN = 1
index = self.comboBox.model().index(
    self.comboBox.currentIndex(), ID_COLUMN, self.comboBox.rootModelIndex()
)
id_ = index.data()
print(id_)
self.label.setText(id_)

Upvotes: 1

Related Questions