我尝试在我的QtreeWidget列中插入项目列表:每个数据都插入到每列中,因此存在与列一样多的项目。 我正在使用MVC架构,但插入失败!添加QtableWidget列中的项目列表
这是我的看法:
def addContact(self, list):#add contact to my QTreeWidget
list=[] #list of contact
items=[]; #list of item
self.treeWidget.setColumnCount(4);
for i in list:
items.append(QtGui.QTreeWidgetItem(list[i])); #create a QtreeWidgetItem's and append them
items.setText(i,items[i])
self.treeWidget.insertTopLevelItem(item[i]) #add all in my tree
class view_dialog(QtGui.QDialog, Ui_Dialog):
def __init__(self):
QtGui.QDialog.__init__(self)
Ui_Dialog.__init__(self)
self.setupUi(self)
def readData(self): #read data entered by user
nom=self.nom_line.text()
prenom=self.prenom_line.text()
tel=self.tel_line.text()
adresse=self.adresse_line.text()
contact=[nom, prenom, tel, adresse]
return contact
def clearData(self): # clear data
self.nom_line.clear()
self.prenom_line.clear()
self.tel_line.clear()
self.adresse_line.clear()
这是我的模型:
class modelContact:
def __init__(self):
self.contact=[] #Create a list of Contact
def AddContact(self, nom, prenom, tel, adresse):#GetContact from my QList
self.contact.append(nom);
self.contact.append(prenom);
self.contact.append(tel);
self.contact.append(adresse);
,这是我的控制器:
def addContactToPhoneBook(self):
list=self.dialog.readData()
self.window.addContact(list)
self.dialog.clearData()
有人能帮助我找到什么是错的?
UDPDATE
我在我的看法改变了我添加的产品清单到QtreeWidget的方式,似乎对我来说更合乎逻辑
我的新观点:
class view_window(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
def addContact(self, list):#add contact to my QTreeWidget
#list=[] #list of contact
items=[]; #list of item
self.treeWidget.setColumnCount(4);
for i in list:
items.append(QtGui.QTreeWidgetItem(i)); #create a QtreeWidgetItem's and append them
for j in range(4):
items.setText(j,items(j))
self.treeWidget.insertTopLevelItem(items) #add all in my tree
但现在我有一个malloc_error_break调试,我认为这是因为我没有分配我的项目,但在Python中,所以我怎么能做一个QTreeWidgetItem * items = new QTreeWidgetItem()与我的项目列表?
'列表= []'确保换循环从不执行,并且没有项目被添加。这行应该被删除。 – mdurant 2014-10-08 22:35:52
嘿thx为您的回应!我删除了行列表[],但我有一个新的问题,该行:items.append(QtGui.QTreeWidgetItem(list [i]))这是正常的,因为列表[i]不是一个整数!那么我该如何添加到我的项目列表中,我的数据联系人列表?或者,也许有另一种方式来做到这一点? – Ary 2014-10-09 08:21:10
让我明白,在addContact中,列表的形式是[nom,prenom,tel,adresse],它们都是字符串,并且您希望将单个行添加到具有此信息的树中? – mdurant 2014-10-09 13:45:32