Я предполагаю, что ваша таблица выглядит следующим образом:
+--------+--------+---------+
|Location|Latitude|Longitude|
+--------+--------+---------+
| A | 11'.22"| 11'.22" |
+--------+--------+---------+
Исходя из приведенного выше кода, я пришел к выводу, что вы хотите, чтобы пользователи выбирали целую строку за раз.Если это так, я бы предложил вам установить следующие свойства для вашего QTableView
:
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
Тогда я бы connect()
до selectionChanged сигнал модели выбора:
connect(ui->tableView, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &),
this, SLOT(onSelectionChanged(const QItemSelection &))));
Вот реализация слота:
void MainWindow::onSelectionChanged(const QItemSelection & selected) {
// Due to selection mode and behavior settings
// we can rely on the following:
// 1. Signal is emited only on selection of a row, not its reset
// 2. It is emited for the whole selected row
// 3. Model selection is always valid and non-empty
// 4. Row indexes of all items in selection match
int rowIndex = selected.indexes().at(0).row();
double latitude = model->index(rowIndex, 1).date().toDouble();
double longitude = model->index(rowIndex, 2).data().toDouble();
// Do whatever you want with the values obtained above
}