在 pyqt5 组合框中显示两个值并传递这些值

displaying two values in a pyqt5 combobox and passing those values

目前我有两个组合框,一个包含 sitecode 另一个 folder,它们是从具有两列 SITECODE 和 [=15= 的 csv 中读取的].任何行都包含这两个相关值。因此,将它们显示为两个单独的组合框是没有意义的。

我想做的是将 SITECODEFOLDER 值仅传递给一个组合框,这样它就会显示为:sitecode | folder

我当前需要合并的代码:

job_list = pd.read_csv(filepath_or_buffer="data/job_list.csv", header=0)
self.sitecodes = job_list[['SITECODE', 'SITECODE']]
self.comboSiteCodes.addItem("", "")
for row in self.sitecodes.itertuples():
    self.comboSiteCodes.addItem(row.SITECODE, row.SITECODE)
self.comboSiteCodes.currentIndexChanged.connect(self.comboSiteCodes_index_changed)

self.jobs = job_list[['FOLDER', 'FOLDER']]
self.comboJobs.addItem("", "")
for row in self.jobs.itertuples():
    self.comboJobs.addItem(row.FOLDER, row.FOLDER)
self.comboJobs.currentIndexChanged.connect(self.comboJobs_index_changed)

值,一旦选择需要更新两个 lineEdit 框,一个用于 sitecode,一个用于 folder

同样,我当前处理此问题的代码:

def comboSiteCodes_index_changed(self, index):
    global comboSiteCodes_gbl
    comboSiteCodes_gbl = f'{self.comboSiteCodes.itemData(index)}'
    self.lineEditSiteCode.setText('{}'.format(comboSiteCodes_gbl))

def comboJobs_index_changed(self, index):
    global comboJobs_gbl
    comboJobs_gbl = f'{self.comboJobs.itemData(index)}'
    self.lineEditJob.setText('{}'.format(comboJobs_gbl))

我一直在谷歌搜索,但找不到这样的用例。

只需使用元组添加项目:

tuples = job_list[['SITECODE', 'FOLDER']]
for row in tuples.itertuples():
    self.combo.addItem('{} | {}'.format(row.SITECODE, row.FOLDER), (row.SITECODE, row.FOLDER))
self.combo.currentIndexChanged.connect(self.comboChanged)

然后以同样的方式访问项目数据:

def comboChanged(self, index):
    code, folder = self.combo.itemData(index)
    # ...