I have a QTableView which is pulling a list of locations and latitude/longitude coordinates from an SQLite database. I want to extract the latitude and longitude from the row that the user has selected in the table, and am using the following code, but it looks fairly convoluted. Maybe I’m just not understanding how to fully utilize Qt’s model/view system. Can I write this code in a clearer, more compact way?
QModelIndexList list1 = this->ui->tableView->selectionModel()->selectedRows(1);
QModelIndexList list2 = this->ui->tableView->selectionModel()->selectedRows(2);
if (list1.count() != 1 || (list1.count() != list2.count()))
{
return;
}
double latitude = list1.at(0).data().toDouble();
double longitude = list2.at(0).data().toDouble();
I assume your table looks like this:
Seen from the code above I conclude that you want your users to select a whole single row at a time.
If it is so I’d suggest you to set the following properties on your
QTableView:Then I’d
connect()to selectionChanged signal of the selection model:Here’s the implementation of the slot: