user7431005
user7431005

Reputation: 4557

Qt table widget, button to delete row

I have a QTableWidget and for all rows I set a setCellWidget at one column to a button.

I would like to connect this button to a function that delets this row. I tried this code, which does not work, because if I simply click my button I do not set the current row to the row of the button.

ui->tableWidget->insertRow(ui->tableWidget->rowCount());
QPushButton *b = new QPushButton("delete",this);
ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1,0,b);
connect(d,SIGNAL(clicked(bool)),this,SLOT(deleteThisLine()));
...


void MainWindow::deleteThisLine()
{
    int row = ui->tableWidget->currentRow();
    ui->tableWidget->removeRow(row);
}

How can I connect my button to a function in a way that the function knows which button (at which row) was pressed?

Upvotes: 3

Views: 5343

Answers (3)

eyllanesc
eyllanesc

Reputation: 244351

To remove the row we must first get the row, if we are inserting widgets inside the cells the currentRow() method will not return the appropriate row, in many cases it will return the row of the last cell without widget that has been selected.

For that reason you must opt for another solution, for this case we will use the indexAt() method of QTableWidget, but for this we need to know the position in pixels of the cell. when one adds a widget to a cell, this cell will be the parent of the widget, so we can access from the button to the cell using the parent() method, and then get the position of the cell with respect to the QTableWidget and use it in indexAt(). To access the button we will use the sender().

When the current cell is removed the focus is lost, a possible solution is to place the focus again in another cell.

void MainWindow::deleteThisLine()
{
    //sender(): QPushButton
    QWidget *w = qobject_cast<QWidget *>(sender()->parent());
    if(w){
        int row = ui->tableWidget->indexAt(w->pos()).row();
        ui->tableWidget->removeRow(row);
        ui->tableWidget->setCurrentCell(0, 0);
    }
}

Upvotes: 3

rakib_
rakib_

Reputation: 142935

Create a custom class, where you pass the created push button object and the row index. From your custom push button class, handle the push button press event and emit a custom signal (it will carry the index number) handled from the object where your custom pushbutton is created. Some related code are below, to give you a hint:

.h

class mypushbutton {
   explicit mypushbutton(QObject *parent = 0, QPushButton *pushbutton = 0, int index = 0);
   signal:
       void deleteRow(int index);
}

.cpp

mypushbutton() {
connect(pushbutton, SIGNAL(clicked(bool)), this, SLOT(actionButtonClick(bool)));
}
actionbuttonclicked() { emit deleteRow(index);}

Upvotes: 0

Farhad
Farhad

Reputation: 4181

Use this connection way to connect signal to a slot:

connect(ui->btnDelete, &QPushButton::clicked, this,&MainWindow::deleteRow);

And delete for example a row on call function:

void MainWindow::deleteRow()
{
    int row = ui->tableWidget->currentRow();
    ui->tableWidget->removeRow(row);
}

Upvotes: 0

Related Questions