求多层表头QTableWidget实例
哪位能够提供多层表头QTableWidget实例,本人在设计一个关于报表管理的小程序,切换报表随时生成GRID进行修改或浏览,谢谢!谢谢!
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QAbstractItemView, QHeaderView from PyQt5.QtCore import Qt app = QApplication([]) # 创建QTableWidget实例 table = QTableWidget() # 设置行列数 table.setColumnCount(3) table.setRowCount(3) # 设置表头 table.setHorizontalHeaderLabels(['Header 1', 'Header 2', 'Header 3']) table.setVerticalHeaderLabels(['', 'Header A', 'Header B']) # 设置表格内容 for i in range(3): for j in range(3): item = QTableWidgetItem('Item ({}, {})'.format(i, j)) table.setItem(i, j, item) # 设置表格属性 table.setEditTriggers(QAbstractItemView.NoEditTriggers) table.setSelectionBehavior(QAbstractItemView.SelectRows) table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) table.verticalHeader().setSectionResizeMode(QHeaderView.Stretch) # 显示表格 table.show() app.exec_()
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QTableWidget, QTableWidgetItem, QHeaderView, QMessageBox from PyQt5.QtCore import Qt class Report: def __init__(self, name, description, date): self.name = name self.description = description self.date = date class ReportManager(QMainWindow): def __init__(self): super().__init__() # 初始化报表列表 self.reports = [] # 设置窗口标题 self.setWindowTitle('Report Manager') # 创建主窗口部件 main_widget = QWidget() self.setCentralWidget(main_widget) # 创建主窗口布局 main_layout = QVBoxLayout() main_widget.setLayout(main_layout) # 创建报表列表部件 report_list_widget = QWidget() main_layout.addWidget(report_list_widget) # 创建报表列表布局 report_list_layout = QHBoxLayout() report_list_widget.setLayout(report_list_layout) # 创建报表列表 self.report_table = QTableWidget() self.report_table.setColumnCount(3) self.report_table.setHorizontalHeaderLabels(['Name', 'Description', 'Date']) self.report_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.report_table.verticalHeader().setVisible(False) report_list_layout.addWidget(self.report_table) # 创建报表操作部件 report_action_widget = QWidget() main_layout.addWidget(report_action_widget) # 创建报表操作布局 report_action_layout = QHBoxLayout() report_action_widget.setLayout(report_action_layout) # 创建报表名称标签和文本框 name_label = QLabel('Name:') self.name_edit = QLineEdit() report_action_layout.addWidget(name_label) report_action_layout.addWidget(self.name_edit) # 创建报表描述标签和文本框 description_label = QLabel('Description:') self.description_edit = QLineEdit() report_action_layout.addWidget(description_label) report_action_layout.addWidget(self.description_edit) # 创建报表日期标签和文本框 date_label = QLabel('Date:') self.date_edit = QLineEdit() report_action_layout.addWidget(date_label) report_action_layout.addWidget(self.date_edit) # 创建添加报表按钮 add_button = QPushButton('Add') add_button.clicked.connect(self.add_report) report_action_layout.addWidget(add_button) # 创建编辑报表按钮 edit_button = QPushButton('Edit') edit_button.clicked.connect(self.edit_report) report_action_layout.addWidget(edit_button) # 创建删除报表按钮 delete_button = QPushButton('Delete') delete_button.clicked.connect(self.delete_report) report_action_layout.addWidget(delete_button) # 更新报表列表 self.update_report_table() def update_report_table(self): # 清空报表列表 self.report_table.setRowCount(0) # 添加报表到列表 for report in self.reports: row = self.report_table.rowCount() self.report_table.insertRow(row) self.report_table.setItem(row, 0, QTableWidgetItem(report.name)) self.report_table.setItem(row, 1, QTableWidgetItem(report.description)) self.report_table.setItem(row, 2, QTableWidgetItem(report.date)) def add_report(self): # 获取报表信息 name = self.name_edit.text() description = self.description_edit.text() date = self.date_edit.text() # 创建报表 report = Report(name, description, date) # 添加报表到列表 self.reports.append(report) # 更新报表列表 self.update_report_table() # 清空文本框 self.name_edit.clear() self.description_edit.clear() self.date_edit.clear() def edit_report(self): # 获取当前选中的报表 selected_rows = self.report_table.selectionModel().selectedRows() # 如果没有选中报表,弹出提示框 if len(selected_rows) == 0: QMessageBox.warning(self, 'Warning', 'Please select a report to edit.') return # 获取选中报表的索引 selected_row = selected_rows[0].row() # 获取选中报表的信息 name = self.name_edit.text() description = self.description_edit.text() date = self.date_edit.text() # 更新选中报表的信息 self.reports[selected_row].name = name self.reports[selected_row].description = description self.reports[selected_row].date = date # 更新报表列表 self.update_report_table() # 清空文本框 self.name_edit.clear