QListWidget四种选择模式
- 连续选择, 快捷键Shift
- 扩展选择,可以Shift,也可以ctrl,最常用
- 多选,一个一个添加,再次点击也可以去除之前添加的
- 不选择
使用的
self.list_wdg = QtWidgets.QListWidget()
# 连续选择, 快捷键Shift
self.list_wdg.setSelectionMode(QtWidgets.QAbstractItemView.ContiguousSelection)
# 扩展选择,可以Shift,也可以ctrl,最常用
self.list_wdg.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
# 多选,一个一个添加,再次点击也可以去除之前添加的
self.list_wdg.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)'''
# 不选择
self.list_wdg.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
槽函数
def print_selected_item(self):
'''
:return:
'''
items = self.list_wdg.selectedItems()
selected_item_labels = []
for item in items:
selected_item_labels.append(item.text())
print 'Selected Items: {0}'.format(selected_item_labels)
完整代码
from PySide2 import QtCore, QtWidgets
from shiboken2 import wrapInstance
import maya.OpenMayaUI as omui
def get_maya_main_window():
'''
Maya main window widget as a pyside2 python object
:return:
'''
maya_main_wnd = omui.MQtUtil.mainWindow()
return wrapInstance(long(maya_main_wnd), QtWidgets.QWidget)
class MultiSelectDialog(QtWidgets.QDialog):
WIDGET_ITEMS = ['Item_00', 'Item_01', 'Item_02', 'Item_03']
def __init__(self, parent=get_maya_main_window()):
super(MultiSelectDialog, self).__init__(parent)
self.setWindowTitle('MultiSelect Dialog UI')
self.setMinimumWidth(200)
self.setWindowFlags(self.windowFlags())
self.create_widgets()
self.create_layouts()
self.create_connections()
QtCore.QMetaObject.connectSlotsByName(self)
def create_widgets(self):
'''
:return:
'''
self.list_wdg = QtWidgets.QListWidget()
# 连续选择, 快捷键Shift
# self.list_wdg.setSelectionMode(QtWidgets.QAbstractItemView.ContiguousSelection)
# 扩展选择,可以Shift,也可以ctrl,最常用
self.list_wdg.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
# 多选,一个一个添加,再次点击也可以去除之前添加的
# self.list_wdg.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
# 不选择
# self.list_wdg.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
for wdg_item in self.WIDGET_ITEMS:
list_wdg_item = QtWidgets.QListWidgetItem(wdg_item)
self.list_wdg.addItem(list_wdg_item)
self.close_btn = QtWidgets.QPushButton("Close")
def create_layouts(self):
btn_layout = QtWidgets.QHBoxLayout()
btn_layout.addStretch()
btn_layout.addWidget(self.close_btn)
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.setContentsMargins(2, 2, 2, 2)
main_layout.setSpacing(2)
main_layout.addWidget(self.list_wdg)
main_layout.addStretch()
main_layout.addLayout(btn_layout)
def create_connections(self):
self.list_wdg.itemSelectionChanged.connect(self.print_selected_item)
self.close_btn.clicked.connect(self.close)
# ___Slots___
def print_selected_item(self):
'''
:return:
'''
items = self.list_wdg.selectedItems()
selected_item_labels = []
for item in items:
selected_item_labels.append(item.text())
print 'Selected Items: {0}'.format(selected_item_labels)
if __name__ == '__main__':
try:
myTest_ui.close() # pylint:disable = E0601
myTest_ui.deleteLater() # pylint:disable = E0601
except:
pass
myTest_ui = MultiSelectDialog()
myTest_ui.show()