QListWidget和多选

32

我有一个普通的QListWidget,其中连接了一些信号和槽。一切都按照我的预期工作。我可以更新、检索、清除等。

但是UI不支持多选。

如何“启用”QListWidget的多选?我的PyQt经验有限告诉我需要通过子类化创建自定义QListWidget,但接下来该怎么做呢?

Google给出了C++的答案,但我正在寻找Python的答案。

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

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

6个回答

35

很遗憾,我无法帮助您处理Python特定的语法问题,但您不需要创建任何子类。

在创建QListWidget后,调用setSelectionMode()并传入多个选择类型之一,可能是QAbstractItemView::ExtendedSelection是您想要的选择类型。此模式有一些变体,您可能需要查看一下。

itemSelectionChanged()信号的槽中,调用selectedItems()以获取指向QListWidgetItem指针的QList


1
extendedSelection。这就是我在寻找的。非常感谢。 - Jeffrey Jose

30

对于PyQT4,它是

QListWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)

对于PySide来说,完全是一样的。 - Darkgaze
1
这应该是被选中的答案,因为OP指定了“Google给我C++的答案,但我正在寻找Python”。对于Qt5,QAbstractItemViewQtWidgets导入。 - mins

14

使用多选功能获取listWidget中的多个选定值的示例。

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_()

输出:

enter image description here


我检查了很多解决方案,但都没有起作用。但是在[Py]QT 5.11.3上运行良好。 - ali reza
感谢您提供示例代码! - MasterEffect117

6
使用PyQt5,您可以通过以下方式将QListWidget的SelectionMode设置为允许多个选择:
from PyQt5 import QtWidgets    


QtWidgets.QListWidget.setSelectionMode(2)

这里是选项的含义:

  • SelectionMode = 0 => NoSelection(不可选择)
  • SelectionMode = 1 => SingleSelection(单选)
  • SelectionMode = 2 => MultiSelection(多选)
  • SelectionMode = 3 => ExtendedSelection(扩展选择)
  • SelectionMode = 4 => ContiguousSelection(连续选择)

参考文献

在Qt Creator中,您可以在以下位置找到此选项: enter image description here


4

此外,你可以使用列表推导式来获取选定的项目,例如

num_ITEMS=[item.text() for item in self.listWidget.selectedItems()]

2

经过长时间的搜索,我发现他们在PyQt6中进行了更改。现在你需要执行以下操作:

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

基本上,您需要从小部件中导入QAbstractItemView并使用正确的选择模式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接