i have this function that deleates the last QLineEdit
widget from the QGridLayout
it checks if the index of the widget is the last one and if the widget is a instance of QLineEdit
---> deleates the widget
def deleate_lastlineedit(self):
widgets = (self.main_layout.itemAt(i).widget() for i in range(self.main_layout.count()))
for index, widget in enumerate(widgets):
if index == (self.main_layout.count()-1) and isinstance(widget, (qtw.QLineEdit,qtw.QLabel)):
widget.deleteLater()
break
I have added a Qlabel widget to the same row and want that the function deleates the last Qlabel and Qlinedit widget at the same time after pushing a button, so far its deleates one at a time, need to click the button two times.
I tried to insert an counter so the iteration stops not at one iteration but at two iterrations so it gets the two widgets but didnt had an effekt.
also inserted two versions of the function one that deleates the qline edit and the other that deleates the qlabel and connected them to the same button but didnt work either
self.getlistof_button.clicked.connect(self.deleate_lastlineedit)
self.getlistof_button.clicked.connect(self.deleate_lastqlabel)
so how can I deleate the two widgets at the same time ?
fullcode
#!/usr/bin/env python
"""
Creates an linedit when button pushed
dleates last linedit
"""
import sys
import sqlite3
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from PyQt5 import QtSql as qsql
from PyQt5 import sip
class AddWidget(qtw.QWidget):
'''
Interface
'''
# Attribut Signal
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# your code will go here
# interface
# position
qtRectangle = self.frameGeometry()
centerPoint = qtw.QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())
# size
self.resize(700, 410)
# frame title
self.setWindowTitle("add Widget")
# heading
heading_label = qtw.QLabel('add Widget')
heading_label.setAlignment(qtc.Qt.AlignHCenter | qtc.Qt.AlignTop)
# add Button
self.addwidget_button = qtw.QPushButton("add Widget")
self.getlistof_button = qtw.QPushButton("deleate")
self.linedittext_button = qtw.QPushButton("linedit text")
self.main_layout = qtw.QGridLayout()
self.main_layout.addWidget(self.getlistof_button,0,0)
self.main_layout.addWidget(self.addwidget_button, 1, 0)
self.main_layout.addWidget(self.linedittext_button, 2, 0)
self.setLayout(self.main_layout)
self.show()
# functionality
self.addwidget_button.clicked.connect(self.add_widget)
self.getlistof_button.clicked.connect(self.deleate_lastlineedit)
self.getlistof_button.clicked.connect(self.deleate_lastqlabel)
self.linedittext_button.clicked.connect(self.count)
def count(self):
x = self.main_layout.rowCount()
print(self.main_layout.rowCount()+1)
print(type(x))
def add_widget(self):
my_lineedit = qtw.QLineEdit()
x1 = (self.main_layout.rowCount()+1)
my_dynmic_label = qtw.QLabel("Dynamic")
self.main_layout.addWidget(my_dynmic_label,x1,0)
self.main_layout.addWidget(my_lineedit,x1,1)
def deleate_lastqlabel(self):
widgets = (self.main_layout.itemAt(i).widget() for i in range(self.main_layout.count()))
for index, widget in enumerate(widgets):
if index == (self.main_layout.count()-1) and isinstance(widget, qtw.QLabel):
# print("yes")
widget.deleteLater()
break
def deleate_lastlineedit(self):
widgets = (self.main_layout.itemAt(i).widget() for i in range(self.main_layout.count()))
for index, widget in enumerate(widgets):
if index == (self.main_layout.count()-1) and isinstance(widget, qtw.QLineEdit):
widget.deleteLater()
break
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = AddWidget()
sys.exit(app.exec_())