67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
'''
|
|
pyqt6 文件列表分组
|
|
'''
|
|
from PyQt6.QtWidgets import QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QListWidget, QGroupBox
|
|
from PyQt6.QtCore import pyqtSignal
|
|
|
|
|
|
class FileListGroup(QGroupBox):
|
|
|
|
load_files = pyqtSignal()
|
|
def __init__(self, parent=None, title="文件列表"):
|
|
super().__init__(title, parent)
|
|
self.init_ui()
|
|
|
|
def init_ui(self):
|
|
layout = QVBoxLayout(self)
|
|
|
|
# 文件列表
|
|
self.file_list = QListWidget()
|
|
self.file_list.setSelectionMode(QListWidget.SelectionMode.MultiSelection)
|
|
layout.addWidget(self.file_list)
|
|
|
|
# 文件列表操作按钮
|
|
file_list_btn_layout = QHBoxLayout()
|
|
|
|
# 加载文件按钮
|
|
self.load_file_btn = QPushButton("加载文件")
|
|
self.load_file_btn.clicked.connect(self.on_load_files)
|
|
|
|
# 全选/全不选按钮
|
|
select_all_btn = QPushButton("全选")
|
|
select_all_btn.clicked.connect(self.on_select_all_files)
|
|
select_none_btn = QPushButton("全不选")
|
|
select_none_btn.clicked.connect(self.on_select_none_files)
|
|
|
|
# 添加按钮到布局
|
|
file_list_btn_layout.addWidget(self.load_file_btn)
|
|
file_list_btn_layout.addWidget(select_all_btn)
|
|
file_list_btn_layout.addWidget(select_none_btn)
|
|
|
|
layout.addLayout(file_list_btn_layout)
|
|
|
|
def on_load_files(self):
|
|
# 通过信号槽机制,添加文件列表
|
|
self.load_files.emit()
|
|
|
|
def on_select_all_files(self):
|
|
# 全选文件
|
|
for i in range(self.file_list.count()):
|
|
item = self.file_list.item(i)
|
|
if item:
|
|
item.setSelected(True)
|
|
|
|
def on_select_none_files(self):
|
|
# 全不选文件
|
|
for i in range(self.file_list.count()):
|
|
item = self.file_list.item(i)
|
|
if item:
|
|
item.setSelected(False)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app = QApplication([])
|
|
app.setStyle("Fusion")
|
|
window = FileListGroup()
|
|
window.show()
|
|
app.exec() |