Closed wcw-xb closed 2 months ago
表格控件TableView TableWidget 默认只能选择一行或者多行,不能选择单个单元格,控件类方法setSelectionBehavior不起作用
Windows 11 专业版
3.11
PySide6 - 6.6.2
PySide6-Fluent-Widgets 1.5.3
import sys from PySide6.QtWidgets import QWidget, QApplication, QTableWidget, QTableWidgetItem, QHBoxLayout from qfluentwidgets import TableWidget class MyWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.initUI() def initUI(self): layout = QHBoxLayout(self) # 初始化 QTableWidget self.table = QTableWidget(self) self.table.setColumnCount(3) # 设置列数 self.table.setHorizontalHeaderLabels(["col1", "col2", "col3"]) # 设置列标题 # 初始化 TableWidget self.table2 = TableWidget(self) self.table2.setColumnCount(3) self.table2.setHorizontalHeaderLabels(["col1", "col2", "col3"]) self.table.setSelectionBehavior(QTableWidget.SelectionBehavior.SelectItems) self.table2.setSelectionBehavior(QTableWidget.SelectionBehavior.SelectItems) for i in range(4): row_position = self.table.rowCount() row_position_2 = self.table2.rowCount() self.table.insertRow(row_position) self.table2.insertRow(row_position_2) self.table.setItem(row_position, 0, QTableWidgetItem(f"row{i}")) self.table.setItem(row_position, 1, QTableWidgetItem(f"row{i}")) self.table.setItem(row_position, 2, QTableWidgetItem(f"row{i}")) self.table2.setItem(row_position_2, 0, QTableWidgetItem(f"row{i}")) self.table2.setItem(row_position_2, 1, QTableWidgetItem(f"row{i}")) self.table2.setItem(row_position_2, 2, QTableWidgetItem(f"row{i}")) layout.addWidget(self.table) layout.addWidget(self.table2) self.setLayout(layout) if __name__ == '__main__': app = QApplication(sys.argv) window = MyWidget() window.show() sys.exit(app.exec())
默认的代理是直接高亮一整行
如果需要改变这个行为需要更换代理
What happened?
表格控件TableView TableWidget 默认只能选择一行或者多行,不能选择单个单元格,控件类方法setSelectionBehavior不起作用
Operation System
Windows 11 专业版
Python Version
3.11
PyQt/PySide Version
PySide6 - 6.6.2
PyQt/PySide-Fluent-Widgets Version
PySide6-Fluent-Widgets 1.5.3
How to Reproduce?
Minimum code