How to know if a row is selected in a QTableView
-
hello guys, my question is simple, how can I know if a row is selected in a QTableView, this is to act according to the selection and delete the selected row, with the Quit button of my form
Here I leave the screenshot of my form:

QObject::connect(ui->btnQuit, &QPushButton::clicked,this, [&](){ if(ui->tvUrl->model()->rowCount()==0){ // ui->btnQuit->setDisabled(true); return; } ui->tvUrl->model()->removeRow(ui->tvUrl->currentIndex().row()); setUpTable(); });I have this code for my Quit button, but I'm not sure if it's the best way to delete a row, thanks in advance for your feedback.
-
@lincoln said in How to know if a row is selected in a QTableView:
how can I know if a row is selected in a QTableView,
Depends a bit on what you mean by "selected."
The current index (QTableView::currentIndex()), is not generally the same thing as the selection that you access through QTableView::selectionModel(). The selection can be a single cell, whole rows, or random cells.
If you want to know which, if any, single cell has the focus then use the current index and be aware it may be a null QModelIndex (i.e. no cell is current).
If you want to know all the cells that are selected then use the selection model.
If you are going to delete multiple rows based on the selection then be careful what order you do it in (high to low row number) and only delete any given row once.