简体   繁体   中英

pyqt : Display All values present in QListWidget as selected to user

I have searched a lot but I am unable to find a solution for doing this.

I have a QListWidget which will populate a list of values. I need to implement a "select all" as default behaviour in the list.

I have already used MultiSelection and that works well, but the need is to avoid clicking on each item, in case the user wants to do the update on all items listed.

Can someone help me understand how to do this?

Use QtWidgets.QAbstractItemView.ExtendedSelection

When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Ctrl key when clicking on an item, the clicked item gets toggled and all other items are left untouched. If the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item. Multiple items can be selected by dragging the mouse over them.

import sys
from PyQt5 import QtWidgets

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    w = QtWidgets.QListWidget()
    for i in range(12):
        w.addItem('Item {}'.format(i))

    w.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)  

    #w.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)  

    w.show()
    sys.exit(app.exec_())

From what I understand you want to implement a function that selects all items, the solution is to iterate using the setSelected() method of QListWidget as shown below:

import sys
from PyQt4 import QtCore, QtGui


class ListWidget(QtGui.QListWidget):
    def __init__(self, parent=None):
        super(ListWidget, self).__init__(parent)
        self.setSelectionMode(QtGui.QListWidget.MultiSelection)

    @QtCore.pyqtSlot()
    def selectAll(self):
        for i in range(self.count()):
            it = self.item(i)
            if it is not None:
                it.setSelected(True)

    @QtCore.pyqtSlot()
    def clearSelection(self):
        for i in range(self.count()):
            it = self.item(i)
            if it is not None:
                it.setSelected(False)


class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        lay = QtGui.QVBoxLayout(self)

        button_sel = QtGui.QPushButton("Select All")
        button_unsel = QtGui.QPushButton("Clear Selection")

        self.list_widget = ListWidget()

        for letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
            it = QtGui.QListWidgetItem(letter)
            self.list_widget.addItem(it)

        button_sel.clicked.connect(self.list_widget.selectAll)
        button_unsel.clicked.connect(self.list_widget.clearSelection)

        lay.addWidget(button_sel)
        lay.addWidget(button_unsel)
        lay.addWidget(self.list_widget)     


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM