qtablewidget插入qcombobox
时间: 2023-10-04 11:09:56 浏览: 155
QTableWidgetTest_QTableWidget_
要在QTableWidget中插入QComboBox,请按照以下步骤操作:
1.创建一个QComboBox对象:
```python
combo_box = QComboBox()
combo_box.addItem("Option 1")
combo_box.addItem("Option 2")
combo_box.addItem("Option 3")
```
2.将QComboBox对象插入到QTableWidget中的单元格中:
```python
table_widget.setCellWidget(row_index, column_index, combo_box)
```
其中,row_index和column_index分别是要插入QComboBox的单元格的行索引和列索引。
完整的示例代码如下所示:
```python
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QComboBox
import sys
app = QApplication(sys.argv)
table_widget = QTableWidget()
table_widget.setRowCount(3)
table_widget.setColumnCount(2)
for row in range(table_widget.rowCount()):
for col in range(table_widget.columnCount()):
item = QTableWidgetItem(f"Item {row+1}-{col+1}")
table_widget.setItem(row, col, item)
combo_box = QComboBox()
combo_box.addItem("Option 1")
combo_box.addItem("Option 2")
combo_box.addItem("Option 3")
table_widget.setCellWidget(1, 1, combo_box)
table_widget.show()
sys.exit(app.exec_())
```
上面的代码将在第2行第2列的单元格中插入一个QComboBox对象。
阅读全文