how to retrive Objects created dynamically

36 views
Skip to first unread message

PBLN RAO

unread,
Sep 3, 2012, 3:54:41 AM9/3/12
to python_in...@googlegroups.com
Hi,

I have a QFrame (Frame_ptr), and at run time i am creating a frames in loop with child objects in that frame.

like
[code]
for i in range (1,5):
    self.frame = QtGui.QFrame(self.Frame_ptr)             # creating frame as child in "Frame_prt" as discussed above

    self.frame.setGeometry(QtCore.QRect(9, self.Record_Location, 575, 25))

    self.frame.setMinimumSize(QtCore.QSize(575, 25))

    self.frame.setMaximumSize(QtCore.QSize(575, 10))

    self.frame.setFrameShape(QtGui.QFrame.NoFrame)

    self.frame.setFrameShadow(QtGui.QFrame.Raised)

    self.frame.setObjectName("Frame_" + i)

    self.frame.show()


#Adding Button

   self.pushButton = QtGui.QPushButton(i,self.frame)                      #creating as child in above created frame

   self.pushButton.setGeometry(QtCore.QRect(0, 0, 65, 25))

   self.pushButton.setAutoFillBackground(False)

   self.pushButton.setCheckable(True)

   self.pushButton.setObjectName("Btn_" +  i)

 
[/code]

now in maya if i use the below command it gives me error
[code]
for child1 in MainUi.Frame_1.children():

    print child1.objectName()


# Error: AttributeError: 'Interface' object has no attribute 'Frame_012' #

[/code]

if i create the same in designer and use the same command i can retrieve the details.
how to overcome this issue.

Justin Israel

unread,
Sep 3, 2012, 11:29:47 AM9/3/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
You are replacing the same self.frame each time you loop

for i in range (1,5):
    self.frame = QtGui.QFrame(self.Frame_ptr)  

This will work in terms of creating them and parenting each button to the frame you just made. 

As a different point, I don't see where you have the attribute name:
MainUi.Frame_1.children
Are you trying to refer directly to the objectName you set dynamically as an attribute? Frame_1 would not automatically resolve to an object name unless you explicitly did:
MainUi.Frame_1 = anObject

PBLN RAO

unread,
Sep 4, 2012, 2:11:40 AM9/4/12
to python_in...@googlegroups.com
Yes justin,

i want to refer to the objectName and other properties like text() that i set dynamically.

How can i do it explicitly...?

Justin Israel

unread,
Sep 4, 2012, 10:45:13 AM9/4/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
If you plan on creating and destroying dynamic objects then you might want to store them in a dict. 

self.dynamics = {}
# create them in a dict
for i in xrange(10):
    obj = QtGui.QWidget(self)
    self.dynamics["widget%02d" % i] = obj

# loop over them later
for name, widget in self.dynamics.iteritems():
    print name, widget

PBLN RAO

unread,
Sep 5, 2012, 4:47:33 AM9/5/12
to python_in...@googlegroups.com
Justin thx for the help....
Reply all
Reply to author
Forward
0 new messages