tckraomuqnt
tckraomuqnt

Reputation: 492

How to update selected item in the proper PyQt5 QLineEdit?

How to update the selected item in the proper QLineEdit? 3 textboxes and filled by various sets of data. If I click some items in QListWidget, every time first QLineedit only updated. Instead of this, I want to update data to the corresponding QLineEdits. ( for Ex: if textbox1 is focused, then the selected item will update in textbox1. If textbox2 is focused, then the selected item will update in textbox2). But In My case, Every time textbox1 is only updated.

import sys

from PyQt5 import QtWidgets
from stackoverflow_file_002 import *

data_1 = ["America","India","Russia","Brazil","Swden","China"]
data_2 = ["Apple","Mango","Grapes","Pine","Berry","Banana"]
data_3 = ["item001","item002","item003","item,004","item005","item006"]

class Example(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Example")
        self.tb1 = QtWidgets.QLineEdit()
        self.tb1.setObjectName("tb1")
        self.tb2 = QtWidgets.QLineEdit()
        self.tb2.setObjectName("tb2")
        self.tb3 = QtWidgets.QLineEdit()
        self.tb3.setObjectName("tb3")
        self.lb1 = QtWidgets.QListWidget()

        self.vbox = QtWidgets.QVBoxLayout()
        self.hbox = QtWidgets.QHBoxLayout()
        self.vbox.addWidget(self.tb1)
        self.vbox.addWidget(self.tb2)
        self.vbox.addWidget(self.tb3)
        self.hbox.addLayout(self.vbox)
        self.hbox.addWidget(self.lb1)
        self.setLayout(self.hbox)

        QtWidgets.QApplication.instance().focusChanged.connect(self.on_focusChanged)

    def on_focusChanged(self):
        fwidget = QtWidgets.QApplication.focusWidget()
        if fwidget is not None:
            focus_widget_name = (fwidget.objectName())

            if focus_widget_name == "tb1":
                self.lb1.clear()
                self.lb1.addItems(data_1)
                self.getdetails_1 = My_File(self.tb1, self.lb1)

            if focus_widget_name == "tb2":
                self.lb1.clear()
                self.lb1.addItems(data_2)
                self.getdetails_1 = My_File(self.tb2, self.lb1)

            if focus_widget_name == "tb3":
                self.lb1.clear()
                self.lb1.addItems(data_3)
                self.getdetails_1 = My_File(self.tb3, self.lb1)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    win = Example()
    win.show()
    sys.exit(app.exec_())

Second File

from PyQt5 import QtWidgets

class My_File(QtWidgets.QWidget):
    def __init__(self,textbox,listbox):
        super().__init__(listbox)
        self.tbox = textbox
        self.lbox = listbox
        self.lbox.clicked.connect(self.getvalue)

    def getvalue(self):
        self.selected_item = self.lbox.currentItem()
        if self.selected_item is not None:
            self.tbox.setText(self.selected_item.text())
            self.tbox.setFocus()
            return True

Upvotes: 1

Views: 222

Answers (1)

ThePyGuy
ThePyGuy

Reputation: 18466

You are passing the same instance of same listbox again and agian to MyFile constructor, and connecting the listBox clicked signal again and again, but connecting the signal works for the first time only, so you need to disconnect if it is already connected, so first try to disconnect the signal if it is connected:

class My_File(QtWidgets.QWidget): 
    def __init__(self,textbox,listbox):
        super().__init__(listbox)
        self.tbox = textbox
        self.lbox = listbox
        try: 
            self.lbox.disconnect()
        except: 
            pass
        self.lbox.clicked.connect(self.getvalue)

Once you do this, everything will work just fine.

Upvotes: 1

Related Questions