N
N
Nikita Presnov2021-08-20 01:22:38
PyQt
Nikita Presnov, 2021-08-20 01:22:38

How to delete a qtablewidget row using a button on that row?

There is a program in pyqt5.
There is such a nice class, it is called from somewhere in the bowels of the program.

import PyQt5.QtWidgets as qtw
import dialog

class blablabla(qtw.QDialog, dialog.Ui_dialog):# dialog.Ui_dialog тупа описание интерфайса диалога
    ...
    def add(self):
        self.table.insertRow(self.table.rowCount())
        self.table.setItem(self.table.rowCount()-1, 0, qtw.QTableWidgetItem("Put smth here"))
        self.table.setItem(self.table.rowCount()-1, 1, qtw.QTableWidgetItem("Put smth here"))
        ...
        btn = qtw.QPushButton("Remove")
        self.table.setCellWidget(self.table.rowCount()-1, self.table.columnCount()-1, btn)

I want to poke at `btn` to delete the corresponding line.
How to implement this online without registration (that is, as concisely as possible and according to Feng Shui)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Presnov, 2021-08-20
@discipuli

Fuck, I banned myself from Google along the way.
In short, here is the solution and the link where I found it

import PyQt5.QtWidgets as qtw
import dialog

class blablabla(qtw.QDialog, dialog.Ui_dialog):# dialog.Ui_dialog тупа описание интерфайса диалога
    ...
    def add(self):
        self.table.insertRow(self.table.rowCount())
        self.table.setItem(self.table.rowCount()-1, 0, qtw.QTableWidgetItem("Put smth here"))
        self.table.setItem(self.table.rowCount()-1, 1, qtw.QTableWidgetItem("Put smth here"))
        ...
        btn = qtw.QPushButton("Remove")
        self.table.setCellWidget(self.table.rowCount()-1, self.table.columnCount()-1, btn)
        btn.clicked.connect(self.removecurrentrow)

    def removecurrentrow(self):
        button = self.sender()
        if button:
            row = self.table.indexAt(button.pos()).row()
            self.table.removeRow(row)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question