0

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

Rouzbeh Zarandi
  • 1,047
  • 2
  • 16
  • 34
  • I guess Form1 already exists when Form2 is created, so why not make it aware of Form1, maybe even as parent, and directly add item to the parent (passed object) list? – mikuszefski Sep 29 '17 at 11:25
  • yes... form 1 needs to be open during all process.....you mean from form2 directly do the event for list in form1? – Rouzbeh Zarandi Sep 29 '17 at 11:45
  • 1
    Exactly. E.g. you create Form2 passing Form1 as parent and then just access parent.itemlist... Check e.g. [here](https://stackoverflow.com/questions/24312425/calling-a-parent-method-from-a-child-widget-in-pyside-pyqt) or [here](https://stackoverflow.com/questions/37918012/pyqt-give-parent-when-creating-a-widget) – mikuszefski Sep 29 '17 at 11:57
  • i initialize the form2 constructor with parent (class Form2(QtGui.QMainWindow): def __init__(self,parent): super(Form2,self).__init__(parent)) and in form1 i called it like self.form_second = Form2(self)... is it right ...if so... how to use directly the itemlist ( self.itemlist.addItem("sfs") doesnt work in FORM2).....:| – Rouzbeh Zarandi Sep 29 '17 at 12:16
  • 1
    You must call the parent object that hat the itemlist property, so something like `self.parent.whateverobject.itemlist.additem(...)` – mikuszefski Sep 29 '17 at 12:20
  • thank you for help <3<3.... my problem solved .... i needed to add self.parent = parent in form2 constructor . and then self.parent.itemlist.addItem() – Rouzbeh Zarandi Sep 29 '17 at 12:36
  • Good to hear that it worked. – mikuszefski Oct 04 '17 at 05:57

1 Answers1

0

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_())
Rouzbeh Zarandi
  • 1,047
  • 2
  • 16
  • 34