QListWidget and Multiple Selection
Asked Answered
M

6

33

I have a regular QListWidget with couple of signals and slots hookedup. Everything works as I expect. I can update, retrieve, clear etc.

But the UI wont support multiple selections.

How do I 'enable' multiple selections for QListWidget? My limited experience with PyQt tells me I need to create a custom QListWidget by subclassing .. but what next?

Google gave me C++ answers but I'm looking for Python

http://www.qtforum.org/article/26320/qlistwidget-multiple-selection.html

http://www.qtcentre.org/threads/11721-QListWidget-multi-selection

Mesh answered 24/10, 2010 at 14:25 Comment(0)
M
36

Unfortunately I can't help with the Python specific syntax but you don't need to create any subclasses.

After your QListWidget is created, call setSelectionMode() with one of the multiple selection types passed in, probably QAbstractItemView::ExtendedSelection is the one you want. There are a few variations on this mode that you may want to look at.

In your slot for the itemSelectionChanged() signal, call selectedItems() to get a QList of QListWidgetItem pointers.

Midi answered 24/10, 2010 at 18:28 Comment(1)
extendedSelection. That's what I was looking for. Thanks a lot.Mesh
C
32

For PyQT4 it's

QListWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
Coimbra answered 3/6, 2014 at 18:19 Comment(2)
For PySide it´s exactly the same.Arnoldarnoldo
This should be the selected answer, as the OP specified "Google gave me C++ answers but I'm looking for Python". For Qt5, QAbstractItemView is imported from QtWidgets.Ingunna
F
15

Example of getting multiple selected values in listWidget with multiple selection.

from PyQt5 import QtWidgets, QtCore
class Test(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Test, self).__init__(parent)
        self.layout = QtWidgets.QVBoxLayout()
        self.listWidget = QtWidgets.QListWidget()
        self.listWidget.setSelectionMode(
            QtWidgets.QAbstractItemView.ExtendedSelection
        )
        self.listWidget.setGeometry(QtCore.QRect(10, 10, 211, 291))
        for i in range(10):
            item = QtWidgets.QListWidgetItem("Item %i" % i)
            self.listWidget.addItem(item)
        self.listWidget.itemClicked.connect(self.printItemText)
        self.layout.addWidget(self.listWidget)
        self.setLayout(self.layout)

    def printItemText(self):
        items = self.listWidget.selectedItems()
        x = []
        for i in range(len(items)):
            x.append(str(self.listWidget.selectedItems()[i].text()))

        print (x)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    form = Test()
    form.show()
    app.exec_()

output :-

enter image description here

Faythe answered 29/8, 2018 at 15:37 Comment(2)
I checked many solutions and didn't work. but this works fine on [Py]QT 5.11.3Bridesmaid
Thank you for providing an example code!Highway
F
7

Using PyQt5 you can set the SelectionMode of your QListWidget to allow multiple selections by using:

from PyQt5 import QtWidgets    


QtWidgets.QListWidget.setSelectionMode(2)

where

  • SelectionMode = 0 => NoSelection
  • SelectionMode = 1 => SingleSelection
  • SelectionMode = 2 => MultiSelection
  • SelectionMode = 3 => ExtendedSelection
  • SelectionMode = 4 => ContiguousSelection

Reference

In Qt Creator you find this option here: enter image description here

Forelimb answered 18/6, 2020 at 14:42 Comment(0)
B
4

In addition, you can use list comprehension to get the selected items, for example

num_ITEMS=[item.text() for item in self.listWidget.selectedItems()]
Bicyclic answered 4/6, 2020 at 21:31 Comment(0)
T
4

After searching for much time I found out they they changed this in PyQt6. Now you have to do the following:

from PyQt6.QtWidgets import QListWidget, QAbstractItemView
# ... all your other imports
class MyWidget(QWidget):
def __init__(self):
    super(MyWidget, self).__init__()
    self.layout = QHBoxLayout()
    self.my_list_view = QListWidget()
    self.my_list_view.setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection) # also try QAbstractItemView.SelectionMode.ExtendedSelection if you want the user to press CTRL for multiple selection

Basically you have to import the QAbstractItemView from the widgets and use the right selection mode

Thiouracil answered 1/12, 2022 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.