在PyQt中,我可以让QListWidget使用QListWidget.setCurrentItem(item)
以编程方式选择一个项目。当然,这将在我的QListWidget中为我选择一个项目。在
但是,我想知道是否存在类似setCurrentItems([item1, item2, item3])
的方法,如果我给出一个列表,它将选择QListWidget中与这些项匹配的所有项。在
现在,我当前的实现只允许我选择一个项目。在本例中,项目“data2”
index = ['data', 'data1', 'data2']
for i in index:
matching_items = listWidget.findItems(i, QtCore.Qt.MatchExactly)
for item in matching_items:
listWidget.setCurrentItem(item)
如果能做这样的事就好了。在
^{pr2}$
除了艾伦斯的回答。您还可以选择:
这将允许您按住
Ctrl
键来打开/关闭项目的选择。除此之外,您还可以按住Shift
键在当前项和单击的项之间切换所有项的选择。在如果您只需要
Shift
键选择功能而不是Ctrl
键选择切换功能,则可以使用:QListWidget
默认情况下支持单个选择,您必须使用setSelectionMode
更改选择模式,在您的情况下:如果要选择
QListWidgetItem
,则必须使用setSelected(True)
。在示例:
^{pr2}$相关问题 更多 >
编程相关推荐