简体   繁体   中英

Dynamically deleting QLineEdit in PyQT4

So I am dynamically creating QLineEdit's from a range:

        for w in range(x):
            s = "s"+str(w)
            self.s = QtGui.QLineEdit(self)
            self.s.setGeometry(QtCore.QRect(15, y, 20, 40))
            self.s.setObjectName(_fromUtf8(s))   
            self.s.show()
            y += 40

I am looking to delete those later. I have tried a bunch of things, but it only deletes the last one I add. I can't find anything explaining why:

for w in range(x):
    s="s"+str(w)
    self.s.deleteLater()

It works for the last one, but none before that. So it will delete the bottom one of the lists.

Any ideas?

for w in range(x):
    s="s"+str(w)
    self.s.deleteLater()

You are setting s to the name of the QLineEdit, but then you're deleting self.s , a different variable altogether, which is set in the first loop to the last created QLineEdit. I think you want something like this:

Creating

self.edits = []
for w in range(x):
    s = "s"+str(w)
    s = QtGui.QLineEdit(self)
    s.setGeometry(QtCore.QRect(15, y, 20, 40))
    s.setObjectName(_fromUtf8(s))   
    s_name.show()
    self.edits.append(s)
    y += 40

Deleting

for s in self.edits:
    s.deleteLater()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM