I'm trying to figure out how to pass values between a list widget and a line edit widget using PySide2. I have a dictionary with three keys and a list with the same values as the three dictionary keys. When the user clicks on the value in the list box I want it to return the value from the dictionary key value pair. I'm able to retrieve the value from the dictionary if I manually enter the key in the code but I can't get it to accept the value from the list widget. Here's the code I'm trying to use:
import sys
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import QLineEdit, QApplication, QMessageBox, QListWidget
from PySide2 import QtWidgets
item_dict = {1: "First Item",
2: "Second Item",
3: "Third Item"}
item_list = [1,2,3]
class myListWidget(QListWidget):
def Clicked(self,item):
click_id = self.item.text()
class myTextWidget(QLineEdit):
def change_text(self):
click_id = myListWidget.Clicked
self.setText = item_dict[click_id]
def main():
app = QApplication(sys.argv)
window = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout(window)
listWidget = myListWidget()
for i in item_dict:
listWidget.addItem(str(i))
TextWidget = myTextWidget()
#listWidget.itemClicked.connect(listWidget.Clicked)
listWidget.itemClicked.connect(myTextWidget.change_text)
layout.addWidget(listWidget)
layout.addWidget(TextWidget)
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
When I click in the list widget I get the following error:
Traceback (most recent call last):
File "C:\Users\Mythranor\Desktop\VN Builder\PyQtTut\listview2.py", line 21, in change_text
self.setText = item_dict[click_id]
KeyError: <function myListWidget.Clicked at 0x000002B6286A8F78>
I have also tried calling the listWidget.item.text directly within the change_text function but it says the listWidget variable is not recognized within the class.