How to add QPushButton in QTableView
-
wrote on 21 Apr 2015, 02:21 last edited by
Dear All,
I am able to add a QStyleOptionButton using delegate inside the QTableView. But i want to add a normal QPushButton because QPushButton can have stylesheets. Kindly let me know how to add QPushButton in QTableView. Thanks -
wrote on 21 Apr 2015, 08:44 last edited by Sam
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 -
wrote on 21 Apr 2015, 12:26 last edited by Hamed
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.
-
wrote on 22 Apr 2015, 02:15 last edited by
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.
. -
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.
wrote on 6 Sept 2017, 12:55 last edited by Christian Spielberger 9 Jun 2017, 14:20@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!