Answer the question
In order to leave comments, you need to log in
How to add text from QLineEdit to QListWidget on button click?
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton,
QHBoxLayout, QVBoxLayout, QApplication, QListWidget, QLineEdit, QListWidgetItem,
QFileDialog)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
Bt_1 = QPushButton("New", self)
Bt_1.move(10,10)
Bt_2 = QPushButton("Mod", self)
Bt_2.move(10,35)
List = QListWidget(self)
List.move(90,10)
Line = QLineEdit(self)
Line. move(10, 60)
Line.resize(70, 20)
Line_2 = QLineEdit(self)
Line_2.move(10, 85)
Line_2.resize(70, 20)
self.setGeometry(300, 300, 400, 400)
self.show()
def on_addClicked():
text = self.Line + " " + Line_2.text()
if len(text):
lvi = QListWidgetItem(self.List)[0]
lvi.setText(0, text)
def delete():
Bt_1.clicked.connect(on_addClicked)
Bt_2.clicked.connect(delete)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Answer the question
In order to leave comments, you need to log in
you haven't saved any widget references. It is better to store the created elements somewhere in order to have access to them.
And adding via the addItem method
QListWidget.addItem
self.button1 = QPushButton('text') # класс родителя можно не передавать, Qt сделает все сам
self.ql = QListWidget()
self.ql.addItem(QListItem('text'))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question