I need to get the index or position of a pushbutton that was created dynamically in pyqt5, I have this code but currently when I click on the [-] buttons I get the position of the last added pushbutton.
import sys
from PyQt5.QtWidgets import (QLineEdit, QLabel, QGridLayout, QWidget,
QPushButton, QApplication, QSpinBox)
class Window(QWidget):
def __init__(self):
super().__init__()
#Creating grid
self.grid = QGridLayout()
self.setLayout(self.grid)
#Creating elements
self.spn_amount = QSpinBox(self)
self.txt_registercode = QLineEdit(self)
self.txt_description = QLineEdit(self)
self.txt_cost = QLineEdit(self)
self.txt_total = QLineEdit(self)
self.btn_add = QPushButton("+",self) #Button that add rows
#adding functionality to [+] button
self.btn_add.clicked.connect(self.getrow_add)
#Adding elements to grid
self.grid.addWidget(self.spn_amount, 0, 0)
self.grid.addWidget(self.txt_registercode, 0, 1)
self.grid.addWidget(self.txt_description, 0, 2)
self.grid.addWidget(self.txt_cost, 0, 3)
self.grid.addWidget(self.txt_total, 0, 4)
self.grid.addWidget(self.btn_add, 0, 5)
self.row = 0
self.column = 0
def getrow_add(self): #Function that get a the actual row of the button to create a new row after
self.row = self.grid.rowCount() #Obtener el número de filas actuales en el grid
self.column = self.grid.columnCount() #Obtiene el número de columnas actuales en el grid
self.addtextbox()
def getrow_del(self):
index = self.grid.indexOf(self.btn_del_din)
position = self.grid.getItemPosition(index)
self.row = position[0] #Obtengo la posición de la fila
self.column = self.grid.columnCount() #Obtiene el número de columnas actuales en el grid
print(self.row)
def addtextbox(self):
self.lineEdits = []
#Creation of dynamicall elements
self.spn_amount_din = QSpinBox(self)
self.txt_registercode_din = QLineEdit(self)
self.txt_description_din = QLineEdit(self)
self.txt_cost_din = QLineEdit(self)
self.txt_total_din = QLineEdit(self)
self.btn_del_din = QPushButton("-",self) #Button that delete rows
#Adding functionality to the [-] button
self.btn_del_din.clicked.connect(self.getrow_del)
#Dynamically adding elements to grid
self.grid.addWidget(self.spn_amount_din, self.row, 0)
self.grid.addWidget(self.txt_registercode_din, self.row, 1)
self.grid.addWidget(self.txt_description_din, self.row, 2)
self.grid.addWidget(self.txt_cost_din, self.row, 3)
self.grid.addWidget(self.txt_total_din, self.row, 4)
self.grid.addWidget(self.btn_del_din, self.row, 5)
self.lineEdits.append('')
def deltextbox(self):
for i in range (1, self.column):
self.grid.itemAtPosition(self.row, i).widget().deleteLater()
if __name__ == "__main__":
application = QApplication(sys.argv)
window = Window()
window.setWindowTitle('Dynamically add elements')
window.show()
sys.exit(application.exec_())
When I click on the [-] buttons I need to get the specific position of that widget.
For example here I have 6 rows and 6 columns, if I click on the first button [-] I would expect to get the value of the row as 1, and the value of the column as 5, however I get the value not from that first button [-] but from the last added, obtained for row 5 and for column 5.
In my code I get that with the function:
def getrow_del(self):
index = self.grid.indexOf(self.btn_del_din)
position = self.grid.getItemPosition(index)
self.row = position[0] #Obtengo la posición de la fila
self.column = self.grid.columnCount() #Obtiene el número de columnas actuales en el grid
print(self.row)