pythonqtpyqtpyqt4pyside

How can I get QListWidget item by name?


I have a QListWidget which displays a list of names using PyQt in Python. How can I get the QListWidgetItem for a given name?

For example, if I have the following QListWidget with 4 items, how can I get the item which contains text = dan?

QListWidget


Solution

  • The python equivalent to vahancho's answer:

    items = self.listWidgetName.findItems("dan",Qt.MatchExactly)
    
    if len(items) > 0:
    
        for item in items:
            print "row number of found item =",self.listWidgetName.row(item)
            print "text of found item =",item.text() 
    

    See documentation here:

    https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QListWidget.html#PySide6.QtWidgets.QListWidget.findItems