How to add QPushButton in QTableView
-
Hi @Sidii,
You can use setIndexWidget(), but this will slow down the loading of tableView if there are too many rows. In such case one approach is to render a pushButton in the paint of delegate and use openPersistentEditor() / closePersistentEditor() when entering/leaving a cell.
Regards
Sam -
this is a part of my old project. a table view that has delete button for every row :
int i = 0; QPushButton *delButton; QStandardItemModel *model; void MainWindow::CreateRowBut() { model->appendRow(new QStandardItem(QString(""))); delButton = new QPushButton(); delButton->setText("Delete " + QString::number(i)); ui->tableView->setIndexWidget(model->index(i , 7), delButton); connect(delButton , SIGNAL(clicked()) , this , SLOT(delete())); i++; }
and the result will be :
still It's kind of impossible to doing this for deleting rows. I struggled with it 2 o 3 days and didn't work out! still if you need it for something else I hop it would work.
-
I would recommend to have delete button outside table and delete selected rows instead of having delete button per row.
As for implementation if you really want to do anything including deleting with specific row it is simple.
Part of the code you can see posted above by Hamed.Problem he is facing is related to the fact that you can't know from the slot called from button click which row (or rather cell index ) caller button belongs. The only way to do it is to iterate through every cell you could set cell widget to and compare it to caller pointer.
Using delegates to display widgets would allow to avoid such overload.
. -
@Hamed I had the same question and found this old thread. After thinking a while about this I had this idea:
Do it in an object oriented way! Define a sub-class of QPushButton which contains all the information you need to delete what you want. Add an "action" slot to your sub-class, connect this slot with the clicked() signal and do the delete in this
slot. Then instead of adding a QPushButton to the model add an object of your sub-class to the model!