I have a Grid layout in which I add Qlineedits at runtime.
while pushing the button I want to delete the last qline edit from the gridlaout
Why does this function delete all qlinedits at the same time ?
def deleate_widgets(self):widgets = (self.main_layout.itemAt(i).widget() for i in range(self.main_layout.count()))for widget in widgets:if isinstance(widget, qtw.QLineEdit):print("linedit: %s - %s" %(widget.objectName(), widget.text()))widget.deleteLater() # all objects
How to change the code to only delete one widget at a time, preferably the last added widget ?
full code
#!/usr/bin/env python"""
Interface to get the specific weight of each of the 5 containers
start_measurment_button starts thread /thread_worker
transfer_data button start query and send data to database
"""import sys
import sqlite3from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from PyQt5 import QtSql as qsqlfrom PyQt5 import sipclass AddWidget(qtw.QWidget):'''Interface with embedded SQL functions'''# Attribut Signaldef __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)# your code will go hereself.mylist = []# interface# positionqtRectangle = self.frameGeometry()centerPoint = qtw.QDesktopWidget().availableGeometry().center()qtRectangle.moveCenter(centerPoint)self.move(qtRectangle.topLeft())# sizeself.resize(700, 410)# frame titleself.setWindowTitle("add Widget")# headingheading_label = qtw.QLabel('add Widget')heading_label.setAlignment(qtc.Qt.AlignHCenter | qtc.Qt.AlignTop)# add Buttonself.addwidget_button = qtw.QPushButton("add Widget")self.getlistof_button = qtw.QPushButton("deleate")self.main_layout = qtw.QGridLayout()self.main_layout.addWidget(self.getlistof_button,0,0)self.main_layout.addWidget(self.addwidget_button, 1, 0)self.setLayout(self.main_layout)self.show()# functionalityself.addwidget_button.clicked.connect(self.add_widget)# self.getlistof_button.clicked.connect(self.deleate_widgets_try)def add_widget(self):self.my_lineedit = qtw.QLineEdit()self.mylist.append(self.my_lineedit)self.main_layout.addWidget(self.my_lineedit)def deleate_widgets(self):widgets = (self.main_layout.itemAt(i).widget() for i in range(self.main_layout.count()))for widget in widgets:if isinstance(widget, qtw.QLineEdit):print(widget)# print("linedit: %s - %s" %(widget.objectName(), widget.text()))# widget.deleteLater() # alle objects# # def deleate_widgets_try(self):# widgets = (self.main_layout.itemAt(i).widget() for i in range(self.main_layout.count()))# my_iter = iter(widgets)# # if isinstance(my_iter, qtw.QLineEdit):# next(my_iter.deleteLater()) # alle objects)if __name__ == '__main__':app = qtw.QApplication(sys.argv)w = AddWidget()sys.exit(app.exec_())