Search code examples
pythonpyqtpyqt5qlineeditqlistwidget

I want to display the QlistWidgets item in QlineEdit onclick of item


I have a QlistWidgets with some data in it and QlineEdit. I want if QlistWidgets item is clicked it should show in QlineEdit. Below is the screenshots

enter image description here

and this is my link to the my project https://github.com/saurav389/Smart_Payroll_Management/blob/master/Department.py

I have tried in pyqt5 on windows

This is My code which add item from database

connection = sqlite3.connect('NewEmployee.db')
c = connection.cursor()
c.execute('SELECT Department FROM Department')
count = 0
for row in c.fetchall():
    item = self.listWidget_DepartView.item(count)
    raw = str(row).replace("('", "").replace("',)", "")
    item.setText(_translate("Dialog", raw))
    count = count + 1
    self.listWidget_DepartView.setSortingEnabled(__sortingEnabled)

Solution

  • QListWidget has a signal called itemClicked() that carries the item that you can use to get the associated text:

        # ...
        self.listWidget_DepartView.itemClicked.connect(self.on_clicked)
        # ...
    
    def on_clicked(self, item):
        self.lineEdit_AddDepart.setText(item.text())
    

    Another possible solution is to use the clicked() signal from QAbstractItemView since QListWidget inherits from that class.

        # ...
        self.listWidget_DepartView.clicked.connect(self.on_clicked)
        # ...
    
    def on_clicked(self, index):
        self.lineEdit_AddDepart.setText(index.data())