Retrieve Location Using Qt Mobility

From Qt Wiki
Jump to: navigation, search

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Retrieve Location Using Qt Mobility


The provided code snippet shows how to retrieve location using Qt Mobility class QGeoPositionInfoSource. When the position is retrieved the location coordinates (latitude and longitude) are displayed on the screen. The given example has been tested on Nokia E7 with Symbian^3.

The following positioning methods can be used:

  • SatellitePositioningMethods - Satellite-based positioning methods such as GPS.
  • NonSatellitePositioningMethods - Other positioning methods.
  • AllPositioningMethods - All positioning methods.

The example application is using NonSatellitePositioningMethods.

Code Snippet

Project File

Qt Mobility should be included at the project file. Additionally if the application is targeting Symbian devices Location should be added as a capability.

symbian:TARGET.CAPABILITY += NetworkServices Location

CONFIG += mobility MOBILITY += location


  1. ifndef MAINWINDOW_H
  2. define MAINWINDOW_H
  1. include <QMainWindow>
  2. include <QGeoPositionInfo>
  3. include <QGeoPositionInfoSource>
  4. include <QLabel>

// QtMobility namespace QTM_USE_NAMESPACE

namespace Ui {

class MainWindow;


class MainWindow : public QMainWindow {



explicit MainWindow(QWidget *parent = 0);

virtual ~MainWindow();

public slots:

* Called when the current position is updated.
* return nothing
void positionUpdated(QGeoPositionInfo geoPositionInfo);


* Start listening for position changes
* return nothing
void startLocationAPI();


QGeoPositionInfoSource* m_pLocationInfo;

QLabel* m_pLabel;


  1. endif // MAINWINDOW_H


  1. include "mainwindow.h"
  1. include <QGeoCoordinate>
  2. include <QApplication>
  3. include <QDesktopWidget>
  1. include <QCoreApplication>
  1. include <QDebug>

MainWindow::MainWindow(QWidget *parent)

: QMainWindow(parent), m_pLocationInfo(NULL), m_pLabel(NULL)


m_pLabel = new QLabel("",this);


MainWindow::~MainWindow() {


void MainWindow::startLocationAPI() {

// Obtain the location data source if it is not obtained already
if (!m_pLocationInfo)
m_pLocationInfo =
//Select positioning method
// When the position is changed the positionUpdated function is called
connect(m_pLocationInfo, SIGNAL (positionUpdated(QGeoPositionInfo)),
this, SLOT (positionUpdated(QGeoPositionInfo)));
// Start listening for position updates


void MainWindow::positionUpdated(QGeoPositionInfo geoPositionInfo) {

if (geoPositionInfo.isValid())
// Get the current location coordinates
QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();
qreal latitude = geoCoordinate.latitude();
qreal longitude = geoCoordinate.longitude();
m_pLabel->setText( QString("Latitude: %1 Longitude: %2").arg(latitude).arg(longitude) );



'QGeoPositionInfo' has not been declared

All required header files must be included and the Qt Mobility namespace should be specified.

  1. include <QGeoPositionInfo>
  2. include <QGeoPositionInfoSource>

// QtMobility namespace QTM_USE_NAMESPACE

  • AllPositioningMethods does not work as expected

This is a known critical bug that affects Qt Mobility 1.1.2 Please check the provided link for details.

See Also