Search code examples
listmodel-view-controllerpyqt4qtreewidgetqtreewidgetitem

Add list of items in QtableWidget columns


I try to insert a list of items in my QtreeWidget columns: each data is inserted in each column so there are as many item as columns. I am using the MVC architecture but the insertion failed!

This is my view:

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()

this is my model:

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);

and this is my controller:

def addContactToPhoneBook(self):
    list=self.dialog.readData()
    self.window.addContact(list)
    self.dialog.clearData()

Someone can help me to find what is wrong?

UDPDATE

I changed in my view the way I add list of items to a QtreeWidget which seems more logical for me

my new view:

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

But now I have a malloc_error_break to debug, and I think it's because I did not allocate my items, but in python so how can I do a QTreeWidgetItem *items= new QTreeWidgetItem() with my list of items??


Solution

  • To add to a single row:

    self.treeWidget.insertTopLevelItem( QtGui.QTreeWidgetItem( list ) )
    

    To add 4 rows, with one datum per row, on successive columns:

    for i in range(4):
        stlist = [""] * 4
        stlist[i] = list[i]
        self.treeWidget.insertTopLevelItem( QtGui.QTreeWidgetItem( stlist ) )