一、基本使用
import sys
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QMainWindow, QListWidget, QAbstractItemView, \
QListWidgetItem
if __name__ == '__main__':
app = QApplication([])
window = QMainWindow()
list_widget = QListWidget(window)
list_widget.resize(500, 300)
# 设置可多选
list_widget.setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection)
# 创建数据项
item = QListWidgetItem()
item.setText("新中国")
# 显示复选框,【复选框的逻辑和列表是脱离的】
item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
# 设置复选框状态(必须设置)
item.setCheckState(Qt.Checked)
list_widget.addItem(item)
window.show()
sys.exit(app.exec())
二、穿梭框实例
效果图
class TemplateSelectWidget(QWidget):
def __init__(self, group_id, parent=None):
super(TemplateSelectWidget, self).__init__(parent=parent)
self.resize(400, 300)
# 组id
self.group_id = group_id
# 未拥有的
self.unselected_list = []
# 已拥有的
self.selected_list = []
# 布局
self.layout = QHBoxLayout(self)
self.setLayout(self.layout)
self.lay = QVBoxLayout(self)
# 两个带复选框的列表
self.unselected_list_widget = QListWidget(self)
# 设置多选
self.unselected_list_widget.setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection)
self.unselected_list_widget.itemSelectionChanged.connect(self.handle_left_list_widget_selection_changed)
self.selected_list_widget = QListWidget(self)
# 设置多选
self.selected_list_widget.setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection)
self.selected_list_widget.itemSelectionChanged.connect(self.handle_right_list_widget_selection_changed)
# 左右穿梭的按钮
button_go_left = QPushButton(self)
button_go_left.setText("<<")
button_go_left.clicked.connect(self.handle_button_left_click)
button_go_right = QPushButton(self)
button_go_right.clicked.connect(self.handle_button_right_click)
button_go_right.setText(">>")
self.lay.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
self.lay.addWidget(button_go_left)
self.lay.addWidget(button_go_right)
self.lay.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
# 垂直布局
self.layout.addWidget(self.unselected_list_widget)
self.layout.addLayout(self.lay)
self.layout.addWidget(self.selected_list_widget)
# 对上级进行模态化
self.setWindowFlags(self.windowFlags() | Qt.Window)
self.setParent(parent)
self.setWindowModality(Qt.WindowModal)
self.init_ui()
self.show()
Index.main_window.template_select = self
def init_ui(self):
self.unselected_list_widget.clear()
self.selected_list_widget.clear()
# 已拥有的
self.selected_list: list[CodeTemplate] = template_list_by_group_id(self.group_id)
# 所有
all_data = [j for j in map(lambda x: x.id, self.selected_list)]
# 未拥有
self.unselected_list: list[CodeTemplate] = [i for i in template_list() if i.id not in all_data]
for template_selected in self.selected_list:
item = QListWidgetItem()
item.setText(template_selected.name)
item.setData(100, template_selected)
item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
item.setCheckState(Qt.Unchecked)
self.selected_list_widget.addItem(item)
for template_unselected in self.unselected_list:
item = QListWidgetItem()
item.setText(template_unselected.name)
item.setData(100, template_unselected)
item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
item.setCheckState(Qt.Unchecked)
self.unselected_list_widget.addItem(item)
def handle_button_left_click(self):
items = self.selected_list_widget.selectedItems()
for item in items:
self.unselected_list.append(item.data(100))
self.selected_list.remove(item.data(100))
# 已拥有的存入数据库
selected: list[CodeTemplateCodeTemplateGroup] = [
CodeTemplateCodeTemplateGroup(code_template_id=item.id, code_template_group_id=self.group_id) for
item in self.selected_list]
template_template_group_del_by_group_id(self.group_id)
template_template_group_adds(selected)
self.init_ui()
def handle_button_right_click(self):
items = self.unselected_list_widget.selectedItems()
for item in items:
self.selected_list.append(item.data(100))
self.unselected_list.remove(item.data(100))
# 已拥有的存入数据库
selected: list[CodeTemplateCodeTemplateGroup] = [
CodeTemplateCodeTemplateGroup(code_template_id=item.id, code_template_group_id=self.group_id) for
item in self.selected_list]
template_template_group_del_by_group_id(self.group_id)
template_template_group_adds(selected)
self.init_ui()
def handle_left_list_widget_selection_changed(self):
for i in range(self.unselected_list_widget.count()):
item = self.unselected_list_widget.item(i)
item.setCheckState(Qt.CheckState.Checked if item.isSelected() else Qt.CheckState.Unchecked)
def handle_right_list_widget_selection_changed(self):
for i in range(self.selected_list_widget.count()):
item = self.selected_list_widget.item(i)
item.setCheckState(Qt.CheckState.Checked if item.isSelected() else Qt.CheckState.Unchecked)