У меня есть сетка, когда я нажимаю sh кнопку, я добавляю QlineEdit
виджет
с помощью этой функции ниже, я получаю содержащийся текст этого виджета редактирования qline и сохраняю его в списке
как сохранить вместо текста ссылку на этот виджет в списке?
цель состоит в том, чтобы иметь возможность удалить этот виджет по этой ссылке
def get_position(self):
layout = self.main_layout
x = layout.itemAtPosition(1,0)
print(x)
# how to get items in last row // referenz
def test_2(self):
for row in range(self.main_layout.rowCount()):
for column in range(self.main_layout.columnCount()):
item = self.main_layout.itemAtPosition(row, column)
if item is not None:
widget = item.widget()
if isinstance(widget, qtw.QLineEdit):
self.myQlistwidget = qtw.QListWidget()
self.myQlistwidget.addItem(widget.text())
items = []
for x in range(self.myQlistwidget.count()):
items.append(self.myQlistwidget.item(x).text())
print(items)
полный код
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from PyQt5 import QtSql as qsql
class AddWidget(qtw.QWidget):
'''
Interface with embedded SQL functions
'''
# 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("get items")
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()
# functionality
self.addwidget_button.clicked.connect(self.add_widget)
self.getlistof_button.clicked.connect(self.get_position)
def get_rowcount(self):
number_rows = self.main_layout
x = number_rows.rowCount()
# get last row
# get all widgets in last row / referenz
# deleate them
print(x)
def get_position(self):
layout = self.main_layout
x = layout.itemAtPosition(1,0)
print(x)
# how to get items in last row // referenz
def test_2(self):
for row in range(self.main_layout.rowCount()):
for column in range(self.main_layout.columnCount()):
item = self.main_layout.itemAtPosition(row, column)
if item is not None:
widget = item.widget()
if isinstance(widget, qtw.QLineEdit):
self.myQlistwidget = qtw.QListWidget()
self.myQlistwidget.addItem(widget.text())
items = []
for x in range(self.myQlistwidget.count()):
items.append(self.myQlistwidget.item(x).text())
print(items)
def add_widget(self):
self.my_lineedit = qtw.QLineEdit()
self.main_layout.addWidget(self.my_lineedit)
# TODO deleate widgets
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = AddWidget()
sys.exit(app.exec_())