Python QListWidget 和多选

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4008649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:49:56  来源:igfitidea点击:

QListWidget and Multiple Selection

pythonuser-interfacepyqtqlistwidget

提问by Jeffrey Jose

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

我有一个经常QListWidget连接的信号和插槽。一切都按我的预期工作。我可以更新,检索,清除等。

But the UI wont support multiple selections.

但是 UI 不支持多选。

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

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

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

谷歌给了我 C++ 答案,但我正在寻找 Python

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

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

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

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

采纳答案by Arnold Spence

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

不幸的是,我对 Python 特定的语法无能为力,但您不需要创建任何子类。

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

您之后QListWidget创建,调用setSelectionMode()与多重选择类型中的一种传递,可能QAbstractItemView::ExtendedSelection是你想要的。您可能需要查看此模式的一些变体。

In your slot for the itemSelectionChanged()signal, call selectedItems()to get a QListof QListWidgetItempointers.

在你的插槽的itemSelectionChanged()信号,打电话selectedItems()得到QListQListWidgetItem指针。

回答by Jeff M.

For PyQT4 it's

对于 PyQT4,它是

QListWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)

回答by Jeff M.

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

使用多项选择在 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_()

output :-

输出 :-

enter image description here

在此处输入图片说明