qtablewidget设置可编辑
时间: 2023-09-28 15:07:35 浏览: 82
要设置QTableWidget可编辑,需要将其属性设置为可编辑,并且需要为每个单元格指定一个编辑器。可以使用setItem()方法为每个单元格设置一个QTableWidgetItem,然后使用setCellWidget()方法为单元格设置一个编辑器。
以下是一个示例代码:
```python
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QComboBox
app = QApplication([])
table = QTableWidget()
table.setColumnCount(2)
table.setRowCount(2)
# 设置可编辑
table.setEditTriggers(QTableWidget.DoubleClicked)
for row in range(table.rowCount()):
for col in range(table.columnCount()):
item = QTableWidgetItem("Item %d-%d" % (row, col))
# 设置单元格
table.setItem(row, col, item)
# 设置编辑器
editor = QComboBox()
editor.addItems(["Option 1", "Option 2", "Option 3"])
table.setCellWidget(row, col, editor)
table.show()
app.exec_()
```
在这个例子中,我们创建了一个2x2的QTableWidget,设置其可编辑,为每个单元格设置了一个QTableWidgetItem,并且为每个单元格设置了一个QComboBox编辑器。