Obtaining a user's location

Qt provides us with a set of functions to retrieve a user's location information, but it will only work if the user's device supports geopositioning. This should work on all modern smartphones and might work on some of the modern computers as well.

To obtain the user's location using the Qt Location module, first let's open up mainwindow.h and add the following header files:

#include <QDebug> 
#include <QGeoPositionInfo> 
#include <QGeoPositionInfoSource> 

After that, declare the following slot function in the same file:

private slots: 
   void positionUpdated(const QGeoPositionInfo &info); 

Right after that, open up mainwindow.cpp and add the following code to the place where you want it to start getting the user's ...

Get Hands-On GUI Programming with C++ and Qt5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.