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 location. For demonstration purposes, I'll just call it within the MainWindow
constructor:
QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(this); if (source) { connect(source, &QGeoPositionInfoSource...