繁体   English   中英

pyqt5 comboBox - 获取选定项目的关联值

[英]pyqt5 comboBox - get the associate value for selected item

在此处输入图像描述

我想设置一个 comboBox 并将我的表的column1数据显示为项目,关联值column2用于选定的项目 ID,并希望在 qLabel 中设置文本。 我正在使用 model 查看 comboBox 中的项目并且工作正常。 我可以获取 currentText 值,但如何获取项目的关联值。 就我而言:我的 sql 表如下:

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

现在,我可以成功地将 column1( type ) 值设置为 comboBox。 现在,如果用户选择值“FEE”,则 qlable 应更新为其关联id :41。怎么做!

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

熊猫模式:

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()

假设 model 将 id 存储在第二列中,那么它必须获取关联索引:

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_)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM