2

Basically I have a delete button for each row in a QTableWidget for the click event.

How do I find the row index of that particular button that get clicked?

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().........?
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
cynric4sure
  • 189
  • 1
  • 12
  • it is exactly like this C++ version, https://stackoverflow.com/questions/46329431/how-to-show-the-row-where-qpushbutton-is-clicked-in-qtablewidget – cynric4sure Jan 22 '19 at 21:43
  • but it looks like the C++ interface is a little bit different from python interface, I cant find similar function in python – cynric4sure Jan 22 '19 at 21:44
  • I am trying to provide a small example, struggling with the formatting – cynric4sure Jan 22 '19 at 21:45
  • the last line is where I had trouble, can not figure out the row index from sender.parent(), according to https://pyqt.readthedocs.io/en/latest/api/qtwidgets/qtablewidget.html indexFromItem takes QTableWidgetItem, but the button is QWidget – cynric4sure Jan 22 '19 at 21:51

1 Answers1

7

Based on your comments, you are using my previous answer as a basis, but as you point out these are failing because the context of the previous question differs from your current code, in the previous case there is a parent widget where the button is set, and that widget is just set in the QTableWidget. In this case, it must be direct:

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • thanks, it works like magic, but how come the indexAt function is nowhere to be found on this page https://pyqt.readthedocs.io/en/latest/api/qtwidgets/qtablewidget.html – cynric4sure Jan 22 '19 at 22:11
  • @cynric4sure I remind you that QTableWidget inherits from QTableView, so it has the methods of the QTableView class will have QTableWidget, so indexAt() can be found in the docs: https://pyqt.readthedocs.io/en/latest/api/qtwidgets/qtableview.html – eyllanesc Jan 22 '19 at 22:13
  • @cynric4sure I recommend reading Qt's docs since it explains what each method is for: http://doc.qt.io/qt-5/qtablewidget.html, http://doc.qt.io/qt-5/qtablewidget-members.html, http://doc.qt.io/qt-5/qabstractitemview.html#indexAt http://doc.qt.io/qt-5/qtableview.html#indexAt – eyllanesc Jan 22 '19 at 22:17
  • thank you very much, does it mean that all functions in C++ have a mirror one in other language api? it looks like only the C++ QT has a comprehensive documentation while python one has minimal. Sorry I am very new to QT – cynric4sure Jan 22 '19 at 22:20
  • @cynric4sure before PyQt provided a copy of the Qt documentation, but at one point it did not, now it seems that it probably does since the page you pointed to only showed a link to the Qt docs of Qt. Actually there is a 99% compatibility between the methods (some methods are not implemented in PyQt), so it is better to read the docs of Qt since it is very descriptive – eyllanesc Jan 22 '19 at 22:23