from PySide6.QtWidgets import QApplication, QWidget, QTableWidget, QTableWidgetItem, QComboBox, QPushButton, QVBoxLayout, QHBoxLayout from PySide6.QtCore import Qt class TableWithComboAndButtons(QWidget): def __init__(self): super().__init__() self.setWindowTitle("表格:两列按钮,某一列下拉框") self.setGeometry(100, 100, 600, 400) # 布局 layout = QVBoxLayout() # 创建表格 self.table = QTableWidget(5, 3) # 5行,3列 self.table.setHorizontalHeaderLabels(["位置", "获取位置", "保存位置"]) # 设置“位置”列为下拉框 for row in range(5): combo = QComboBox() combo.addItems(["位置 A", "位置 B", "位置 C"]) self.table.setCellWidget(row, 0, combo) # 设置“获取位置”和“保存位置”列为按钮 for row in range(5): get_button = QPushButton("获取位置") get_button.clicked.connect(self.get_location) self.table.setCellWidget(row, 1, get_button) save_button = QPushButton("保存位置") save_button.clicked.connect(self.save_location) self.table.setCellWidget(row, 2, save_button) # 将表格添加到布局中 layout.addWidget(self.table) self.setLayout(layout) def get_location(self): # 获取当前行的“位置”列的内容 selected_row = self.table.currentRow() if selected_row >= 0: location = self.table.cellWidget(selected_row, 0).currentText() print(f"获取位置: {location}") else: print("没有选中任何行") def save_location(self): # 获取当前行的“位置”列的内容并保存 selected_row = self.table.currentRow() if selected_row >= 0: location = self.table.cellWidget(selected_row, 0).currentText() print(f"保存位置: {location}") self.table.setItem(selected_row, 0, QTableWidgetItem(f"已保存 - {location}")) else: print("没有选中任何行") if __name__ == "__main__": app = QApplication([]) window = TableWithComboAndButtons() window.show() app.exec()