Reputation: 131
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.
Because of the view it doesnt delete the previous values. Resulting to something like this in the photo.
What I tried so far:
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
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