Double_Mind
Double_Mind

Reputation: 89

(PyQt5) Create list of QLineEdit

How to make a list of line editors without many variables? (smth like self.line_1 = QLineEdit(self), self.line_2 = QLineEdit(self), ... , self.line_9000 = QLineEdit(self))

For example, I want to create this

list of QLineEdit

window with ability to get access to each element. A simple cycle does not provide access to each element, only last. How I can do this?

Upvotes: 1

Views: 1973

Answers (2)

AlexanderFokin
AlexanderFokin

Reputation: 61

layout = QFormLayout()    
self.alphabet_line_edits = dict.fromkeys(['а', 'б', 'в', 'г'])
for letter in self.alphabet_line_edits:
    line_edit = QLineEdit()
    layout.addRow(letter, line_edit)
    self.alphabet_line_edits[letter] = line_edit

def button_clicked(self):
    print(self.alphabet_line_edit['б'].text())

Upvotes: 0

Mike
Mike

Reputation: 131

One way is to make widgets as you said - cycle, and you can access to the widget with using layout.itemAtPosition

it would go like this :

layout = QVBoxLayout()
for i in range(list_length):
    line_edit = QLineEdit(self)
    layout.addWidget(line_edit)

to access the widget :

def access_widget(int):
    item = layout.itemAtPosition(int)
    line_edit = item.widget()
    return line_edit

now you can access to the designated QLineEdit.

Upvotes: 1

Related Questions