PDA

View Full Version : Remove row button in QTabelview



guidupas
1st March 2014, 15:06
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->insertRow(modelo->rowCount());
modelo->setData(modelo->index(modelo->rowCount()-1,1), 1);
tabela->setItemDelegateForColumn(1, spinBoxDelegar);
tabela->setIndexWidget(modelo->index(modelo->rowCount()-1,2), excluirButton);
tabela->edit(modelo->index(modelo->rowCount()-1,0));

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)
{
this->modelo1->removeRow(linha);
}
10092

anda_skoa
1st March 2014, 21:15
One thing you can try is to have one signal mapper and store all button mappings in there.

Then, when you remove a row, you iterate over the mapping numbers, retrieve the sender object, remove the mapping for that object and create a new mapping with the row the button is now in.

Cheers,
_

guidupas
10th March 2014, 15:31
Could you help me with some code?

Thanks