Remove row button in QTableview

  • Hello all!

    I am having some problems with a QPushButton used inside a QTableview. This button needs to remove the row but when I remove one row its changes the index and the button starts to remove the wrong row.

    How can I pass the right parameter to void "MainWindow::on_excluir_clicked(int linha)" ?

    Thanks a lot.

    Code below:
    void MainWindow::adicionarLinha(QStandardItemModel *modelo, QTableView *tabela)
    spinBoxDelegar = new spinBoxDelegate(this);

    QPushButton *excluirButton = new QPushButton(*iconeExcluirButton, "", 0);
    excluirButton = new QPushButton(*iconeExcluirButton, "", 0);
    excluirButton->setStyleSheet("background-color: rgb(255, 255, 255); color: rgb(255, 255, 255); border: 0");
    QSignalMapper *mapearSinalBotaoExcluir = new QSignalMapper(this);
    modelo->setData(modelo->index(modelo->rowCount()-1,1), 1);
    tabela->setItemDelegateForColumn(1, spinBoxDelegar);
    tabela->setIndexWidget(modelo->index(modelo->rowCount()-1,2), excluirButton);
    mapearSinalBotaoExcluir->setMapping(excluirButton, modelo->rowCount()-1);
    connect(excluirButton, SIGNAL(clicked()), mapearSinalBotaoExcluir, SLOT(map()));
    connect(mapearSinalBotaoExcluir, SIGNAL(mapped(int)), this, SLOT(on_excluir_clicked(int)));


    void MainWindow::on_excluir_clicked(int linha)

  • Lifetime Qt Champion


    Every time you remove a row, the other will have their index changed so your buttons will be completely off.

    You should rather scan your table until you find the line with the button you just clicked. Look for sender() and indexWidget().

    Hope it helps

  • Could you help me with some code?


  • Lifetime Qt Champion

    Do you mean the for loop that goes from row 0 to count() -1 ?

  • No. I would like to know how to compare the two objects to know if they are the same.

    Thanks you for the answers.

  • Lifetime Qt Champion

    You can simply compare the pointer returned by sender() to the pointer given by indexWidget()

  • SGaist, thank you very much. It worked. Very big help.

  • Lifetime Qt Champion

    You're welcome !

    If this solves your question, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

  • Hello
    Could you share the solution. I have the same issue, but I am using pyqt.


  • Hello @Legnain !

    Here it goes:

    void MainWindow::on_excluir_clicked1()
        QPushButton  *clicado = qobject_cast<QPushButton *>(sender());
        for(int i = 0; i < this->modelo1->rowCount(); i++)
            if(clicado == this->ui->tableView->indexWidget(this->modelo1->index(i,5)))

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.