求助多层表头锁定任意列的事例
求助多层表头锁定任意列的事例,能否赐一份,谢谢!
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QHeaderView from PyQt5.QtCore import Qt class MyTableWidget(QTableWidget): def __init__(self, parent=None): super().__init__(parent) self.setColumnCount(5) self.setRowCount(4) self.setHorizontalHeaderLabels(['', 'A', 'B', 'C', 'D']) # 设置表头 self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.verticalHeader().setVisible(False) self.horizontalHeader().setSectionsClickable(False) self.horizontalHeader().setFixedHeight(60) self.horizontalHeader().setFixedWidth(600) # 设置第一列的宽度和锁定 self.setColumnWidth(0, 100) self.horizontalHeader().setSectionResizeMode(0, QHeaderView.Fixed) self.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch) # 设置第一行和第二行表头 self.setSpan(0, 1, 1, 2) self.setSpan(1, 1, 1, 2) self.setItem(0, 0, QTableWidgetItem('')) self.setItem(1, 0, QTableWidgetItem('')) self.setItem(0, 1, QTableWidgetItem('Group 1')) self.setItem(1, 1, QTableWidgetItem('A')) self.setItem(1, 2, QTableWidgetItem('B')) self.setItem(0, 3, QTableWidgetItem('Group 2')) self.setItem(1, 3, QTableWidgetItem('C')) self.setItem(1, 4, QTableWidgetItem('D')) # 设置单元格内容 self.setItem(2, 0, QTableWidgetItem('1')) self.setItem(2, 1, QTableWidgetItem('1')) self.setItem(2, 2, QTableWidgetItem('2')) self.setItem(2, 3, QTableWidgetItem('3')) self.setItem(2, 4, QTableWidgetItem('4')) self.setItem(3, 0, QTableWidgetItem('2')) self.setItem(3, 1, QTableWidgetItem('5')) self.setItem(3, 2, QTableWidgetItem('6')) self.setItem(3, 3, QTableWidgetItem('7')) self.setItem(3, 4, QTableWidgetItem('8')) # 设置锁定列的背景色 for i in range(self.rowCount()): item = self.item(i, 0) item.setBackground(Qt.lightGray) item.setFlags(Qt.ItemIsEnabled) if __name__ == '__main__': app = QApplication([]) table = MyTableWidget() table.show() app.exec_()