Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1002779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:52:39+00:00 2026-05-16T07:52:39+00:00

I have a QTableView which is pulling a list of locations and latitude/longitude coordinates

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:52:39+00:00Added an answer on May 16, 2026 at 7:52 am

    I assume your table looks like this:

    +--------+--------+---------+
    |Location|Latitude|Longitude|
    +--------+--------+---------+
    |   A    | 11'.22"| 11'.22" |
    +--------+--------+---------+
    

    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:

    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    

    Then I’d connect() to selectionChanged signal of the selection model:

    connect(ui->tableView, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &),
            this,          SLOT(onSelectionChanged(const QItemSelection &))));
    

    Here’s the implementation of the slot:

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a QTableView which displays data from a QSqlTableModel. I want my model
I have a tableview which loads data for an sqlite database and displays them
I have a basic QTableView displaying data from an SQLite table with QSqlTableModel. Data
I am developing and tableView in which in each row I have to display
I have a QTableView that I need to get the selectionChanged event from. I
I have tableview which holds a list of data, when users press Edit i
I have a tableview which shows a webview on clicking some row in the
I am using a QTableView and QSqlTableModel to show data from a sqlite DB.
I have an tableview which is loaded from a mutablearray as listed below. However
I have a tableview which displays an image that is obtained from a server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.