How to show the row where QPushButton is clicked i

2019-02-25 14:26发布

问题:

I would like to delete row where QPushButton is clicked how it is possible to I think it is reasonable to use slots but how to do it don't know , if you have any ideas how to get a row of selected button please share, thanks.

It is my table

It is a code where i add rows to my QTableWidget

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    for(int i = 0; i<20;i++)
        ui->tableWidget->insertRow(ui->tableWidget->rowCount());
    QVector<QString>vec;
    vec<<"first"<<"sec"<<"third"<<"for"<<"fif"<<"first"<<"sec"
      <<"third"<<"for"<<"fif";
    vec<<"first"<<"sec"<<"third"<<"for"<<"fif"<<"first"<<"sec"
      <<"third"<<"for"<<"fif";
    for(int i = 0; i<ui->tableWidget->rowCount();i++)
    {
        for(int j = 0; j<ui->tableWidget->columnCount();j++)
        {
            if(j == 0)
            {
                QWidget* pWidget = new QWidget();
                QPushButton* btn_edit = new QPushButton();
                btn_edit->setText("Remove");
                QHBoxLayout* pLayout = new QHBoxLayout(pWidget);
                pLayout->addWidget(btn_edit);
                pLayout->setAlignment(Qt::AlignCenter);
                pLayout->setContentsMargins(0, 0, 0, 0);
                pWidget->setLayout(pLayout);
                ui->tableWidget->setCellWidget(i, j, pWidget);
                continue;

            }
            QTableWidgetItem*item = new QTableWidgetItem(vec[i]);
            item->setFlags(item->flags() ^ Qt::ItemIsEditable);
            ui->tableWidget->setItem(i, j, item);
        }
    }
}

回答1:

This task can be solved using the removeRow() method but before we must get the row. First of all we will connect all the buttons to a slot inside the loop as shown below:

*.h

private slots:
    void onClicked();

*.cpp

[...]
QPushButton* btn_edit = new QPushButton();
btn_edit->setText("Remove");
connect(btn_edit, &QPushButton::clicked, this, &MainWindow::onClicked);
[...]

In the slot we can get the button that emits the signal through the sender() method, then we get QWidget parent (created with the name pWidget), this is the widget that is added to the QTableWidget and its position is relative to it, then we use the method indexAt() to get the QModelIndex associated with the cell, and this has the information of the row through the method row(). All of the above is implemented in the following lines:

void MainWindow::onClicked()
{
    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);
    }
}

Note: The setCurrentCell() method is used to set the focus since the last cell that has it has been deleted.

The complete example can be found in the following link.



回答2:

When you are creating QPushButton just add :

    btn_delete = new QPushButton("Remove", ui->tableWidget);
    btn_delete->setObjectName(QString("%1").arg(ui->tableWidget->rowCount()));

    connect(btn_delete, SIGNAL(clicked()), this,    SLOT(CellButtonDeleteClicked()));

And create function CellButtonDeleteClicked()

     void CellButtonDeleteClicked()
     {

      //   by this line I can get the sender of signal
      QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());

      int row = pb->objectName().toInt();
      ui->tableWidget->removeRow(row);
     }