按钮如何删除它所在的 QTableWidget 中的一行
How does a button delete a row in a QTableWidget where it sits
基本上我在 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().........?
根据您的评论,您正在使用 my previous answer 作为基础,但正如您指出的那样,这些都失败了,因为前一个问题的上下文与您当前的代码不同,在前一个案例中有一个设置按钮的父小部件,并且该小部件只是在 QTableWidget 中设置。在这种情况下,它必须是直接的:
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_())
基本上我在 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().........?
根据您的评论,您正在使用 my previous answer 作为基础,但正如您指出的那样,这些都失败了,因为前一个问题的上下文与您当前的代码不同,在前一个案例中有一个设置按钮的父小部件,并且该小部件只是在 QTableWidget 中设置。在这种情况下,它必须是直接的:
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_())