我们想在操作列表内添加按钮,例如查看、修改、删除
1、定义添加按钮的方法
1 # 列表内添加按钮 2 def buttonForRow(self,id): 3 widget=QWidget() 4 # 修改 5 updateBtn = QPushButton(‘修改‘) 6 updateBtn.setStyleSheet(‘‘‘ text-align : center; 7 background-color : NavajoWhite; 8 height : 30px; 9 border-style: outset; 10 font : 13px ‘‘‘) 11 12 updateBtn.clicked.connect(lambda:self.updateTable(id)) 13 14 # 查看 15 viewBtn = QPushButton(‘查看‘) 16 viewBtn.setStyleSheet(‘‘‘ text-align : center; 17 background-color : DarkSeaGreen; 18 height : 30px; 19 border-style: outset; 20 font : 13px; ‘‘‘) 21 22 viewBtn.clicked.connect(lambda: self.viewTable(id)) 23 24 # 删除 25 deleteBtn = QPushButton(‘删除‘) 26 deleteBtn.setStyleSheet(‘‘‘ text-align : center; 27 background-color : LightCoral; 28 height : 30px; 29 border-style: outset; 30 font : 13px; ‘‘‘) 31 32 33 hLayout = QHBoxLayout() 34 hLayout.addWidget(updateBtn) 35 hLayout.addWidget(viewBtn) 36 hLayout.addWidget(deleteBtn) 37 hLayout.setContentsMargins(5,2,5,2) 38 widget.setLayout(hLayout) 39 return widget
2、在向tableWidget里添加数据时插入即可
1 for row_number, row_data in enumerate(rsdata): 2 self.ui.tableWidget.insertRow(row_number) 3 for i in range(len(row_data)+1): 4 if i<len(row_data): 5 self.ui.tableWidget.setItem(row_number, i, QtWidgets.QTableWidgetItem(str(row_data[i]))) 6 # 添加按钮 7 if i==len(row_data): 8 # 传入当前id 9 self.ui.tableWidget.setCellWidget(row_number, i,self.buttonForRow(str(row_data[0])))
效果图
时间: 2024-10-12 08:09:37