Qt table widget, button to delete row

巧了我就是萌 提交于 2019-12-13 14:03:02

问题


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?


回答1:


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);
    }
}



回答2:


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);
}



回答3:


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);}


来源:https://stackoverflow.com/questions/46641743/qt-table-widget-button-to-delete-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!