I have a combobox which shows 2 values a
| b
these are taken from a csv file,
The normal implementation works with out issue, see this post for more details: displaying two values in a pyqt5 combobox and passing those values
However, I have a button to append a row to the csv, I then want the combobox to be updated with the new csv contents. So, obviously I first clear the values already loaded into the combo box.
In the below code the issue seems to be self.comboSiteCodes.clear()
, if I remove it the code functions successfully, although it appends the updated csv to the end of the combobox list giving duplication.
Where have I gone wrong?
def add_job(self):
code, good = QInputDialog.getText(self, '\n', 'Input Site Code')
if good:
folder, good = QInputDialog.getText(self, '\n', 'Input Folder Prefix')
if good:
with open("data/job_list.csv", "a") as myfile:
myfile.write("\n" + code + ',' + folder)
# self.update_site_folder_combo()
self.comboSiteCodes.clear()
job_list = pd.read_csv(filepath_or_buffer="data/job_list.csv", header=0)
tuples = job_list[['SITECODE', 'FOLDER']]
for row in tuples.itertuples():
self.comboSiteCodes.addItem('{} | {}'.format(row.FOLDER, row.SITECODE), (row.FOLDER, row.SITECODE))
self.comboSiteCodes.currentIndexChanged.connect(self.comboSiteCodes_index_changed)
Just as a wrap up, the problem was not in combobox.clear()
You should check if your combobox is connected to any kind of function. If that is the case, you should switch that connection before and after clearing your combobox
self.comboSiteCodes.disconnect(...)
self.comboSiteCodes.clear()
...
self.comboSiteCodes.connect(...)