-1

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Spatial Digger
  • 1,883
  • 1
  • 19
  • 37
  • please provide a [mre] – eyllanesc Aug 23 '21 at 07:50
  • what is self? How do you populate your combobox initially? format of the csv? – CarlosSR Aug 23 '21 at 08:05
  • @eyllanesc normally I would, but this uses pyqt5 so it will take a lot to add a gui. The csv is a simple 2 column file using headers `SITECODE` and `FOLDER`. – Spatial Digger Aug 23 '21 at 08:07
  • @CarlosSR self is just the usual indicator to use the constructor value. In this case `self.comboSiteCodes` is the combobox. As I said, if I do not clear the combobox the code works, although it appends the new data to the end of the current combobox content. – Spatial Digger Aug 23 '21 at 08:10
  • 1
    i meant how you define your class in here... is your combobox connected to any function? try self.comboSiteCodes.disconnect() before your clear statement and self.comboSiteCodes.connect() after you repopulate your combo – CarlosSR Aug 23 '21 at 08:16
  • @SpatialDigger Your question is unclear to me: is the problem that even after calling `self.comboSiteCodes.clear()` the combo is not cleared out? That said, the fact that it uses pyqt5 is completely pointless, your code requires no more than 4-5 controls, so that's probably less than 15-20 lines; on the other hand, we cannot help you at all if you don't provide us a valid MRE. – musicamante Aug 23 '21 at 08:20

1 Answers1

1

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(...)
CarlosSR
  • 1,145
  • 1
  • 10
  • 22