List 在QtableWidget列中添加项目列表
我尝试在我的QtreeWidget列中插入一个项目列表:每个数据都插入到每个列中,因此有尽可能多的项目作为列。 我正在使用MVC架构,但插入失败 我的看法是:List 在QtableWidget列中添加项目列表,list,model-view-controller,pyqt4,qtreewidget,qtreewidgetitem,List,Model View Controller,Pyqt4,Qtreewidget,Qtreewidgetitem,我尝试在我的QtreeWidget列中插入一个项目列表:每个数据都插入到每个列中,因此有尽可能多的项目作为列。 我正在使用MVC架构,但插入失败 我的看法是: def addContact(self, list):#add contact to my QTreeWidget list=[] #list of contact items=[]; #list of item self.treeWidget.setColumnCount(4); for i in l
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()
self.treeWidget.insertTopLevelItem( QtGui.QTreeWidgetItem( list ) )
要在连续列上添加4行,每行一个基准,请执行以下操作:
for i in range(4):
stlist = [""] * 4
stlist[i] = list[i]
self.treeWidget.insertTopLevelItem( QtGui.QTreeWidgetItem( stlist ) )
list=[]
确保for循环永远不会执行,也不会添加任何项。这行应该被删除。嘿,谢谢你的回复!我删除了行列表[],但该行有一个新问题:items.append(QtGui.QTreeWidgetItem(list[I]),这是正常的,因为list[I]不是整数!那么我如何才能添加到我的项目列表,我的数据联系人列表??或者还有其他方法吗?请让我了解,在addContact中,列表的形式为[nom,prenom,tel,adresse],它们都是字符串,您希望在树中添加一行信息吗?是的,但我希望每列中有一项!在我的树中添加一个循环的家长列表!后者可以用insertTopLevelItems
一行完成,非常感谢!你让我开心;)