How to list all items from QListWidget
Asked Answered
F

3

8

I apology if it has been already asked but would you please clarify it again: how to get all ListWidgetItems listed in QListWidget?

Poster later:

Here it's in action. There are 5 items in a list. Subtracting one results 4.

from PyQt4 import QtGui, QtCore

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

        self.lw = QtGui.QListWidget()
        myBoxLayout.addWidget(self.lw)

        for i in range(5):
            QtGui.QListWidgetItem('myItem', self.lw)

        ok_button = QtGui.QPushButton("Print count")
        ok_button.clicked.connect(self.OK)      
        myBoxLayout.addWidget(ok_button) 

    def OK(self):
        # let self.lw haven elements in it.
        items = []
        for x in range(self.lw.count()-1):
            items.append(self.lw.item(x))
        print len(items)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    sys.exit(app.exec_())
Franny answered 22/3, 2014 at 0:12 Comment(0)
E
7

Here is a easy way to get all ListWidgetItems in a listWidget.

lw = QtGui.QListWidget()
# let lw haven elements in it.
items = []
for x in range(lw.count()-1):
    items.append(lw.item(x))

#items will consist a list of ListWidgetItems.
Enesco answered 22/3, 2014 at 2:4 Comment(4)
Thanks! Exactly what was needed! I am not sure if I want to subtract -1 from lw.count(). What is a reason?Franny
@Sputnix : because index's start from 0 not from 1 , so what count gives you is the count of elements in the row, which is always 1 more than your index.Enesco
I am afraid it is not so. Try to run a code posted in OM.Franny
range(x) generates a range from 0 to (x-1), so the code will not add the last item to the list. It should be "for x in range(lw.count())"Shanly
P
11

Here is a pythonic way to implement this:

lw = QtGui.QListWidget()
items = [lw.item(x) for x in range(lw.count())]

Or if you want a list of strings:

lw = QtGui.QListWidget()
items = [lw.item(x).text() for x in range(lw.count())]
Pluri answered 13/4, 2021 at 9:35 Comment(0)
E
7

Here is a easy way to get all ListWidgetItems in a listWidget.

lw = QtGui.QListWidget()
# let lw haven elements in it.
items = []
for x in range(lw.count()-1):
    items.append(lw.item(x))

#items will consist a list of ListWidgetItems.
Enesco answered 22/3, 2014 at 2:4 Comment(4)
Thanks! Exactly what was needed! I am not sure if I want to subtract -1 from lw.count(). What is a reason?Franny
@Sputnix : because index's start from 0 not from 1 , so what count gives you is the count of elements in the row, which is always 1 more than your index.Enesco
I am afraid it is not so. Try to run a code posted in OM.Franny
range(x) generates a range from 0 to (x-1), so the code will not add the last item to the list. It should be "for x in range(lw.count())"Shanly
F
4

Extracting values from a QlistWidget Object

def Extract(self):
    lst = QtGui.QListWidget()
    items = []
    for x in range(lst.count()):
        items.append(lst.item(x).text())
    print(items) 
Fondue answered 13/8, 2020 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.