QTableView: Add a button to each row of the table to get the row number.
Solved
General and Desktop
-
I want to add a button at the end of each row which on pressed will display a message "The Row No is <no.>".
I have added a button but I can't figure out how can I retrieve the row number.
Here's my code about how I added the buttonsfor(int i = 0;i<model->rowCount();i++) { QPushButton *cartButton = new QPushButton(); QPixmap pixmap(":/test/info.png"); QIcon buttonIcon(pixmap); ui->tableView->setIndexWidget(model->index(i,2),cartButton); cartButton->setIcon(buttonIcon); }
Here's how the table is form.
I just want to know what changes I must do to my code so that whenever I press the button, I get a popup message displaying the row number.
I use Qt creator 6.3.1 on windows 10.
-
Hi
One way is to use a lambda as slot and capture a copy of I for the connectfor(int i = 0;i<model->rowCount();i++) { ...... ...... connect(cartButton , &QPushButton::clicked, [ i ]() { qDebug()<<"clicked" << i ; }); .....