Solved QListWidget signal in pyside2 and attach a dictionary to listwidget
-
Hello,
i have multiple queries regarding QListWidget due to lack of examples and experience looking of suggestions and options, with code will be appreciated- when i select an item(key) from the listwidget it should display data(value) stored in it in texteditwid without any looping
- want to use only dict to display data on widget as per model/view want to know its possible, if yes will appreciate examples
- this query relates partially to the first one, if the dict option isn't handy enough kindly suggest provide an example of loop with itemselected signal because whenever i try i prompts Qt native signal
-
I think this is what you are asking for, I included an example of dict use in it..
I'm also new to this but I made this window by the following code:
from PySide2.QtWidgets import * from PySide2.QtGui import * from PySide2.QtCore import * import sys app = QApplication() w = QMainWindow() central = QWidget(w) list = QListWidget(central) list.addItems(["key1","key2","key3"]) mydata = {"key1":"data1", "key2":"data2", "key3":"data3"} text = QTextEdit(central) ###### This is what you will be interested in ######## list.itemClicked.connect(lambda: text.setText((mydata[list.currentItem().text()]))) ############################################# lay = QHBoxLayout(central) lay.addWidget(list) lay.addWidget(text) central.setLayout(lay) w.setCentralWidget(central) w.show() exitcode = app.exec_() sys.exit(exitcode)
I think I should clarify a bit more, The listwidget fires a lot of signals when you select / interact with items like itemClicked, itemActivated, itemEntered etc.. and most of them send a Qlistwidget item. So:
- get a slot/function that takes in a Qlistwidgetitem
- make the function get key from the listwidget item by using .text().
- use the key to get data from ur dict.
- settext of texteditwidget to the data that u have gotten.
-
Superb @Erudite-Monkey,
This is what i was looking forI am working in a 3d package Maya
the reference code i created wasfrom PySide2 import QtWidgets from PySide2 import QtGui from PySide2 import QtCore test_lay = QtWidgets.QVBoxLayout() test_lay_wid = QtWidgets.QWidget() test_parentwid = QtWidgets.QWidget() test_button = QtWidgets.QPushButton() test_listwid = QtWidgets.QListWidget(test_parentwid) #QtWidgets.QListWidgetItem(testing_print.out_var, test_listwid) QtWidgets.QListWidgetItem("Test_01", test_listwid) QtWidgets.QListWidgetItem("Test_02", test_listwid) QtWidgets.QListWidgetItem("Test_03", test_listwid) test_lay.addWidget(test_parentwid) test_lay.addWidget(test_button) test_lay_wid.setLayout(test_lay) test_listwid.insertItem(10, QtWidgets.QListWidgetItem("Test_04")) test_lay_wid.show() def print_someting(): print str(test_listwid.currentItem().text()), "apna time aayega" #test_listwid.currentItem().text() test_listwid.itemClicked.connect(print_someting)
which i figured out yesterday night :)
I rarely have used lambda but with your eg i'll even learn something better- when i select an item(key) from the listwidget it should display data(value) stored in it in texteditwid without any looping -- checked
- want to use only dict to display data on widget as per model/view want to know its possible, if yes will appreciate examples -- checked
You nailed it man
didn't even require the last point
3) this query relates partially to the first one, if the dict option isn't handy enough kindly suggest provide an example of loop with itemselected signal because whenever i try i prompts Qt native signalThank You, @Erudite-Monkey