deku
deku

Reputation: 131

PyQT Deleting QVBoxLayout In Order To Accomodate Updated Values from DB

So I have a problem with my PyQT5 interface is that when I load values from the database, it duplicates the in the view as it is being looped after button click.

Here is the code the populates a view when an item is inserted from the DB, it takes values from the DB. And displays it via loop.

def restart_program(self):
    total, items = fetch_items()
    for item in items:
        item = str(item[0]) + ' - ' + str(item[2]) +'x'
        self.b3 = QtWidgets.QPushButton(item)
        self.v_box.addWidget(self.b3)
        self.b3.clicked.connect(self.btn_click1)
    curr_budget = fetch_budget()
    curr_budget = curr_budget[0]
    self.message2.setText("Total: " + str(total))
    self.budget_status.setText("Budget: " + str(curr_budget))

    self.message3.setText(" ")

The problem here is that.

image

Because of the view it doesnt delete the previous values. Resulting to something like this in the photo.

What I tried so far:

  1. Getting the items and their frequencies and put them in a dictionary
    • Clearly didnt work as it just populated the db
  2. had an idea to clear the view QVBoxLayout before so that when the view laods the data from db again, it wouldn't show the past inputs

But I'm not sure on how to implement #2. My full code can be seen here in the so_revision.py file

Upvotes: 1

Views: 49

Answers (1)

tmdag
tmdag

Reputation: 529

You could check how many elements you already have in your QVBoxLayout and remove them (just be careful not to remove your label etc) for eg:

def restart_program(self):
    total, items = fetch_items()

    for i in range(1, self.v_box.count()):
        existing_item = self.v_box.itemAt(i).widget()
        if existing_item:
            existing_item.setParent(None)

    for item in items:
        item = str(item[0]) + ' - ' + str(item[2]) +'x'
        self.b3 = QtWidgets.QPushButton(item)

        self.v_box.addWidget(self.b3)
        self.b3.clicked.connect(self.btn_click1)
    curr_budget = fetch_budget()
    curr_budget = curr_budget[0]
    self.message2.setText("Total: " + str(total))
    self.budget_status.setText("Budget: " + str(curr_budget))

    self.message3.setText(" ")

Upvotes: 1

Related Questions