import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QPushButton class MainWindow(QMainWindow): def __init__(self): super().__init__() # 创建 TableWidget self.table_widget = QTableWidget(self) self.setCentralWidget(self.table_widget) # 设置表格的行数和列数 self.table_widget.setRowCount(4) # 设置表格有4行 self.table_widget.setColumnCount(2) # 设置表格有2列 # 添加按钮控件到每个单元格 for row in range(self.table_widget.rowCount()): # 遍历每一行 for col in range(self.table_widget.columnCount()): # 遍历每一列 # 创建按钮控件,设置文本并连接槽函数 button = QPushButton("按钮", self.table_widget) button.clicked.connect(self.button_clicked) # 按钮点击事件处理函数 # 将按钮控件添加到单元格中 self.table_widget.setCellWidget(row, col, button) # 设置单元格的其他内容 item = QTableWidgetItem(f"行{
pyqt6 给tablewidget中添加按钮控件,超详细
最新推荐文章于 2025-02-08 20:18:37 发布