Implementing area monitoring in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:Abstract It is assumed here that you have set up Qt Mobility in your development environment and on your device. For more information, see CS001615 - Setting up Qt Mobility.

Template:ArticleMetaData

Qt project file[edit | edit source]

Link the Location module into the project:

CONFIG += mobility MOBILITY = location

Using the Location module requires the Location capability:

symbian: {

   TARGET.CAPABILITY = Location

}

Header[edit | edit source]

  1. include <qgeoareamonitor.h>
  2. include <qgeopositioninfo.h>

// QtMobility namespace QTM_USE_NAMESPACE

class MainWindow : public QMainWindow {

   Q_OBJECT

public slots:

   /**
    * Called when the current position is in range of the area.
    */
   void areaEntered(const QGeoPositionInfo &update);
   /**
    * Called when the current position moves out of range of the area.
    */
   void areaExited(const QGeoPositionInfo &update);

private:

   /**
    * Initializes the area monitor.
    */
   void initAreaMonitor();

}

Source[edit | edit source]

  1. include <qgeocoordinate.h>

void MainWindow::initAreaMonitor() {

   // Create the area monitor
   QGeoAreaMonitor *monitor = QGeoAreaMonitor::createDefaultMonitor(this);
   // Connect the area monitoring signals to the corresponding slots
   connect(monitor, SIGNAL(areaEntered(QGeoPositionInfo)),
           this, SLOT(areaEntered(QGeoPositionInfo)));
   connect(monitor, SIGNAL(areaExited(QGeoPositionInfo)),
           this, SLOT(areaExited(QGeoPositionInfo)));
   qreal latitude = 60.169966;
   qreal longitude = 24.952115;
   QGeoCoordinate myLocation(latitude, longitude);
   monitor->setCenter(myLocation);
   monitor->setRadius(100);

}

void MainWindow::areaEntered(const QGeoPositionInfo &update) {

   printString("The area has been entered.");

}

void MainWindow::areaExited(const QGeoPositionInfo &update) {

   printString("The area has been exited.");

}

Postconditions[edit | edit source]

A specific area is defined and monitored.

See also[edit | edit source]