Search code examples
pythonpython-2.7pyqt4

python pyqt4 add item to the Qlist from another class


i have 2 forms (form1.ui & form2.ui). form1 contains a list for products and a pushbutton of new product. the new product button opens a new form2 which has a editline and a insert button .the insert button calls a method from another class(AllMethods class) which inserts the content of editline content to the list in form1..... the error is allmethod instance has no attribute itemlist(name of Qlist)

import sys
from PyQt4 import QtGui, uic 

class AllMethods:

    def addListMethod(self,itm):
        itemList.addItem(itm)

#_________FORM2____________
class Form2(QtGui.QMainWindow):
    def __init__(self):
        super(Form2,self).__init__()
        uic.loadUi('Form2.ui',self)
        self.show()

        self.insertItem.clicked.connect(self.insertItem_clicked)

    def insertItem_clicked(self):
        obj = AllMethods()
        obj.addListMethod(self.itemname.text())

#_________FORM1____________
class Form1(QtGui.QMainWindow):
    def __init__(self):
        super(Form1,self).__init__()
        uic.loadUi('Form1.ui',self)
        self.show()

        self.newItem.clicked.connect(self.newItem_clicked)


    def newItem_clicked(self):
        self.form_second = Form2()
        self.form_second.show()

#_________________MAIN____________________  
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = Form1()
    sys.exit(app.exec_())

i tried to call Form1 class like

mainF = Form1()
mainF.itemlist.addItem(itm)

but another form1 pops up and dissapear in a second and the opened form1 remains same empty.

actually this is the small program that illustrates my problem in the main program i have.

img


Solution

  • thanks @mikuszefski using only two classes of form1,form2(form2 method add items to list in form1)

    import sys
    from PyQt4 import QtGui, uic 
    
    #_________FORM2____________
    class Form2(QtGui.QMainWindow):
        def __init__(self,parent):
            super(Form2,self).__init__(parent)
            uic.loadUi('Form2.ui',self)
            self.show()
    
            self.parent = parent
            self.insertItem.clicked.connect(self.insertItem_clicked)
    
        def insertItem_clicked(self):   
            self.parent.itemList.addItem(self.itemname.text())
    
    #_________FORM1____________
    class Form1(QtGui.QMainWindow):
        def __init__(self):
            super(Form1,self).__init__()
            uic.loadUi('Form1.ui',self)
            self.show()
    
            self.newItem.clicked.connect(self.newItem_clicked)
    
    
        def newItem_clicked(self):
            self.form_second = Form2(self)
            self.form_second.show()
    
    #_________________MAIN____________________  
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        window = Form1()
        sys.exit(app.exec_())
    

    using three classes of form1,form2,AllMethods ( form2 calls method in AllMethods class and insert to the list in form1 )

    import sys
    from PyQt4 import QtGui, uic 
    
    class AllMethods:
    
        def __init__(self,parent):
            self.parent = parent
    
        def addListMethod(self,itm):
            self.parent.itemList.addItem(itm)
    
    
    #_________FORM2____________
    class Form2(QtGui.QMainWindow):
        def __init__(self,parent):
            super(Form2,self).__init__(parent)
            uic.loadUi('Form2.ui',self)
            self.show()
    
            self.parent = parent
            self.insertItem.clicked.connect(self.insertItem_clicked)
    
        def insertItem_clicked(self):
            obj = AllMethods(self.parent)
            obj.addListMethod(self.itemname.text())
    
    #_________FORM1____________
    class Form1(QtGui.QMainWindow):
        def __init__(self):
            super(Form1,self).__init__()
            uic.loadUi('Form1.ui',self)
            self.show()
    
            self.newItem.clicked.connect(self.newItem_clicked)
    
    
        def newItem_clicked(self):
            self.form_second = Form2(self)
            self.form_second.show()
    
    #_________________MAIN____________________  
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        window = Form1()
        sys.exit(app.exec_())