QT4 表格的指定单元格中添加按钮
时间: 2024-02-09 22:08:29 浏览: 83
要在QT4表格的指定单元格中添加按钮,可以使用QTableWidgetItem类中的QTableWidget::setCellWidget方法。以下是一个简单的示例代码,用于在QT4表格的指定单元格中添加按钮:
```python
from PyQt4.QtGui import QTableWidgetItem, QPushButton, QTableWidget, QApplication, QMainWindow, QVBoxLayout, QWidget
class AppDemo(QWidget):
def __init__(self):
super().__init__()
# 创建表格
table = QTableWidget(5, 3)
table.setHorizontalHeaderLabels(['Name', 'Age', 'Button'])
# 在指定单元格中添加按钮
button = QPushButton(self)
button.setText("Click me")
table.setCellWidget(2, 2, button)
# 创建布局并添加表格
layout = QVBoxLayout(self)
layout.addWidget(table)
self.setLayout(layout)
# 创建应用程序
app = QApplication([])
demo = AppDemo()
demo.show()
app.exec_()
```
在这个例子中,我们创建了一个QTableWidget,并在第3行第3列(即索引为2,2)的单元格中添加了一个按钮。我们还为表格添加了列标题。在应用程序中,我们创建了一个AppDemo类,并将QTableWidget添加到QWidget布局中。最后,我们显示了应用程序并启动了事件循环。
阅读全文
相关推荐















