QListWidget and Multiple Selection

41,074

Solution 1

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.

Solution 2

For PyQT4 it's

QListWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)

Solution 3

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

Solution 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()]
Share:
41,074
Jeffrey Jose
Author by

Jeffrey Jose

Updated on June 21, 2020

Comments