所以基本上我有一个删除按钮,用于点击事件的QTableWidget中的每一行。
如何找到被点击的特定按钮的行索引?
class WidgetGallery(QDialog):
def __init__(self, parent=None):
super(WidgetGallery, self).__init__(parent)
self.table = QTableWidget(10, 3)
col_1 = QTableWidgetItem("first_col")
col_2 = QTableWidgetItem("second_col")
deleteButton = QPushButton("delete_this_row")
deleteButton.clicked.connect(self.deleteClicked)
self.table.setItem(0, 0, col_1)
self.table.setItem(0, 1, col_2)
self.table.setCellWidget(0, 2, deleteButton)
self.mainLayout = QGridLayout()
self.mainLayout.addWidget(self.table)
def deleteClicked(self):
sender = self.sender()
row = sender.parent().........?
from PyQt5 import QtCore, QtGui, QtWidgets
class WidgetGallery(QtWidgets.QDialog):
def __init__(self, parent=None):
super(WidgetGallery, self).__init__(parent)
self.table = QtWidgets.QTableWidget(10, 3)
col_1 = QtWidgets.QTableWidgetItem("first_col")
col_2 =QtWidgets.QTableWidgetItem("second_col")
deleteButton = QtWidgets.QPushButton("delete_this_row")
deleteButton.clicked.connect(self.deleteClicked)
self.table.setItem(0, 0, col_1)
self.table.setItem(0, 1, col_2)
self.table.setCellWidget(0, 2, deleteButton)
self.mainLayout = QtWidgets.QGridLayout(self)
self.mainLayout.addWidget(self.table)
@QtCore.pyqtSlot()
def deleteClicked(self):
button = self.sender()
if button:
row = self.table.indexAt(button.pos()).row()
self.table.removeRow(row)
if name == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = WidgetGallery()
w.show()
sys.exit(app.exec_())
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。