I created a QListWidget
in QtDesigner and i want to add it some items from another list called "list_1" that i created before . The thing is that, that list(list_1
) is updating every time i press a combination of keys.
Here is the code:
class Win(self):
list_1 = []
number = 0 #This is a variable that is continuously changing
def __init__(self):
#Some stuff in here
def addItem(self):
item = "Number " + str(Win.number)
Win.list_1.append(item)
Win.number += 1
class Dialog(QDialog):
def __init__(self):
QDialog._init__(self):
uic.loadUi("ListWidget.ui", self)
def addItems(self):
#I want to create this method to add the items from list_1 into the `QListWidget`
How can i do to open the QDialog
(with a button that i already have) and see in the QListWidget the items as they are appended in the list. I mean, "i open the QDialog
and i see Number 1. Then, i close the QDialog
, add a number to the list and when i open the QDialog
again i can see Number 1
and Number 2
, and so on.
Hope you can help me.
If you're not going to use the Model/View architecture, you have two options.
Unless you have thousands of items or have complex custom painting for each of the items, this will be pretty fast.
class Dialog(QDialog):
def __init__(self, list_1):
QDialog._init__(self):
uic.loadUi("ListWidget.ui", self)
for txt in list_1:
QListWidgetItem(txt, self.list_widget)
Don't even bother keeping a separate list. Directly edit the QListWidgetItems
in the QListWidget
. This only works if you're not destroying and creating the QDialog
every time.
In the long run, I think you'll find this method works better. You'll be tearing down and refreshing the GUI less (which means it will be faster), and you won't have to deal with bugs where the source list was updated but not the GUI (and vice versa).