Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QListWidget signal in pyside2 and attach a dictionary to listwidget
Forum Updated to NodeBB v4.3 + New Features

QListWidget signal in pyside2 and attach a dictionary to listwidget

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    blossomsg
    wrote on last edited by
    #1

    Hello,
    i have multiple queries regarding QListWidget due to lack of examples and experience looking of suggestions and options, with code will be appreciated

    1. when i select an item(key) from the listwidget it should display data(value) stored in it in texteditwid without any looping
    2. want to use only dict to display data on widget as per model/view want to know its possible, if yes will appreciate examples
    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 signal
    1 Reply Last reply
    0
    • Erudite MonkeyE Offline
      Erudite MonkeyE Offline
      Erudite Monkey
      wrote on last edited by Erudite Monkey
      #2

      I think this is what you are asking for, I included an example of dict use in it..

      alt text
      alt text
      alt text

      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:

      1. get a slot/function that takes in a Qlistwidgetitem
      2. make the function get key from the listwidget item by using .text().
      3. use the key to get data from ur dict.
      4. settext of texteditwidget to the data that u have gotten.
      1 Reply Last reply
      0
      • B Offline
        B Offline
        blossomsg
        wrote on last edited by
        #3

        Superb @Erudite-Monkey,
        This is what i was looking for

        I am working in a 3d package Maya
        the reference code i created was

        from 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

        1. when i select an item(key) from the listwidget it should display data(value) stored in it in texteditwid without any looping -- checked
        2. 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 signal

        Thank You, @Erudite-Monkey

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved