qtablewidgetitem添加按钮
时间: 2023-04-28 09:01:18 浏览: 676
您可以使用QTableWidgetItem的setFlags()方法将其设置为可编辑,并使用setCellWidget()方法将QPushButton添加到单元格中。以下是示例代码:
```python
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QPushButton
app = QApplication([])
table = QTableWidget(3, 3)
for i in range(3):
for j in range(3):
item = QTableWidgetItem("Item %d,%d" % (i, j))
item.setFlags(item.flags() | Qt.ItemIsEditable)
button = QPushButton("Button %d,%d" % (i, j))
table.setCellWidget(i, j, button)
table.setItem(i, j, item)
table.show()
app.exec_()
```
相关问题
c++ qtablewidgetitem添加按钮
您可以使用自定义的QTableWidgetItem来添加按钮到QTableWidget中。以下是一个示例代码:
```cpp
#include <QApplication>
#include <QTableWidget>
#include <QPushButton>
#include <QTableWidgetItem>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTableWidget tableWidget(3, 2);
for(int row = 0; row < tableWidget.rowCount(); ++row)
{
for(int col = 0; col < tableWidget.columnCount(); ++col)
{
QPushButton *button = new QPushButton("Click me");
QTableWidgetItem *item = new QTableWidgetItem();
// 设置单元格对齐方式为居中
item->setTextAlignment(Qt::AlignCenter);
// 将按钮设置为单元格的小部件
tableWidget.setCellWidget(row, col, button);
// 将自定义的QTableWidgetItem添加到单元格中
tableWidget.setItem(row, col, item);
}
}
tableWidget.show();
return app.exec();
}
```
这段代码创建了一个3x2的表格,并在每个单元格中添加了一个按钮。您可以根据您的需求修改表格的大小和按钮的文本等属性。
在Qtablewidget中添加按钮
在QTableWidget中添加按钮需要使用自定义单元格小部件。下面是一个例子,展示如何在QTableWidget中添加按钮:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QPushButton, QWidget, QHBoxLayout
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建表格
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(2)
self.tableWidget.setColumnCount(2)
self.setCentralWidget(self.tableWidget)
# 添加按钮到表格中
for row in range(2):
for column in range(2):
button = QPushButton("Button")
button.clicked.connect(self.buttonClicked)
widget = QWidget()
layout = QHBoxLayout(widget)
layout.addWidget(button)
layout.setAlignment(Qt.AlignCenter)
layout.setContentsMargins(0, 0, 0, 0)
widget.setLayout(layout)
self.tableWidget.setCellWidget(row, column, widget)
def buttonClicked(self):
print("Button clicked")
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
在这个例子中,我们首先创建了一个QTableWidget,并设置它的行数和列数。然后,我们在表格中循环遍历每个单元格,为每个单元格创建一个QPushButton,并将其添加到一个QWidget中。我们还使用QHBoxLayout将QPushButton居中,并将QWidget设置为单元格的小部件。最后,我们将QWidget添加到QTableWidget中的单元格中。
我们还连接了每个按钮的clicked信号到buttonClicked函数,当按钮被点击时,该函数将被调用。在这个例子中,我们只是简单地打印了一条消息。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)