Need help getting user permission for location coordinates in Qt 6.5
-
Hello everyone,
I've been struggling to obtain the user's location coordinates in my application for the past few days.
I initially encountered issues with the code provided in the documentation, which caused my application to crash. Upon further research, I discovered that I needed user permission. However, all the available resources in the week seem to be explaining how to get permissions using QGeoPositionInfoSource for Qt 5 and none of them are detailing the new method of using QPermissions in Qt 6.5, the latest version of Qt.
Furthermore, I am unable to include the QPermissions class in my application even after linking Qt Core.
Could someone please assist me in this matter? Any help or guidance would be much appreciated.
Note that I am using a licensed version of Qt 6.5. -
Hello everyone,
I've been struggling to obtain the user's location coordinates in my application for the past few days.
I initially encountered issues with the code provided in the documentation, which caused my application to crash. Upon further research, I discovered that I needed user permission. However, all the available resources in the week seem to be explaining how to get permissions using QGeoPositionInfoSource for Qt 5 and none of them are detailing the new method of using QPermissions in Qt 6.5, the latest version of Qt.
Furthermore, I am unable to include the QPermissions class in my application even after linking Qt Core.
Could someone please assist me in this matter? Any help or guidance would be much appreciated.
Note that I am using a licensed version of Qt 6.5.@Aninoss said in Need help getting user permission for location coordinates in Qt 6.5:
Furthermore, I am unable to include the QPermissions class in my application even after linking Qt Core.
What exactly happens?
https://doc.qt.io/qt-6/qpermission.html shows how to request location permission:QLocationPermission locationPermission; locationPermission.setAccuracy(QLocationPermission::Precise); qApp->requestPermission(locationPermission, this, &LocationWidget::permissionUpdated); void LocationWidget::permissionUpdated(const QPermission &permission) { if (permission.status() != Qt::PermissionStatus:Granted) return; auto locationPermission = permission.value<QLocationPermission>(); if (!locationPermission || locationPermission->accuracy() != QLocationPermission::Precise) return; updatePreciseLocation(); }
-
@Aninoss said in Need help getting user permission for location coordinates in Qt 6.5:
Furthermore, I am unable to include the QPermissions class in my application even after linking Qt Core.
What exactly happens?
https://doc.qt.io/qt-6/qpermission.html shows how to request location permission:QLocationPermission locationPermission; locationPermission.setAccuracy(QLocationPermission::Precise); qApp->requestPermission(locationPermission, this, &LocationWidget::permissionUpdated); void LocationWidget::permissionUpdated(const QPermission &permission) { if (permission.status() != Qt::PermissionStatus:Granted) return; auto locationPermission = permission.value<QLocationPermission>(); if (!locationPermission || locationPermission->accuracy() != QLocationPermission::Precise) return; updatePreciseLocation(); }
@jsulm said in Need help getting user permission for location coordinates in Qt 6.5:
Furthermore, I am unable to include the QPermissions class in my application even after linking Qt Core.
I encountered an error message after including the QPermissions library in my project. I'm unsure why this is happening - is it possible that CMake is unable to locate the library in the Qt installation? Or perhaps the library is not included in the Qt package at all?
even wen I'm inluding the Qt Core library
cmake_minimum_required(VERSION 3.16) project(positioning VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOMOC ON) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.2 COMPONENTS Core Positioning Quick REQUIRED) qt_add_executable(apppositioning main.cpp ) qt_add_qml_module(apppositioning URI positioning VERSION 1.0 QML_FILES Main.qml ) set_target_properties(apppositioning PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_link_libraries(apppositioning PRIVATE Qt6::Core Qt6::Quick Qt6::Positioning ) install(TARGETS apppositioning BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
-
@jsulm said in Need help getting user permission for location coordinates in Qt 6.5:
Furthermore, I am unable to include the QPermissions class in my application even after linking Qt Core.
I encountered an error message after including the QPermissions library in my project. I'm unsure why this is happening - is it possible that CMake is unable to locate the library in the Qt installation? Or perhaps the library is not included in the Qt package at all?
even wen I'm inluding the Qt Core library
cmake_minimum_required(VERSION 3.16) project(positioning VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOMOC ON) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.2 COMPONENTS Core Positioning Quick REQUIRED) qt_add_executable(apppositioning main.cpp ) qt_add_qml_module(apppositioning URI positioning VERSION 1.0 QML_FILES Main.qml ) set_target_properties(apppositioning PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_link_libraries(apppositioning PRIVATE Qt6::Core Qt6::Quick Qt6::Positioning ) install(TARGETS apppositioning BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
-
@jsulm said in Need help getting user permission for location coordinates in Qt 6.5:
It's QPermission not QPermissions
But The Qt documentation specifies using
#include <QPermissions>
to include the QPermissions library,
but even after making this change#include <QPermission>
, I'm still encountering issues with it.D:\sandBox\positioning\main.cpp:3: error: QPermission: No such file or directory D:\sandBox\positioning\main.cpp:3:10: fatal error: QPermission: No such file or directory 3 | #include <QPermission> | ^~~~~~~~~~~~~
Any ideas on what could be causing this problem?"
-
@jsulm said in Need help getting user permission for location coordinates in Qt 6.5:
It's QPermission not QPermissions
But The Qt documentation specifies using
#include <QPermissions>
to include the QPermissions library,
but even after making this change#include <QPermission>
, I'm still encountering issues with it.D:\sandBox\positioning\main.cpp:3: error: QPermission: No such file or directory D:\sandBox\positioning\main.cpp:3:10: fatal error: QPermission: No such file or directory 3 | #include <QPermission> | ^~~~~~~~~~~~~
Any ideas on what could be causing this problem?"
-
@jsulm said in Need help getting user permission for location coordinates in Qt 6.5:
Furthermore, I am unable to include the QPermissions class in my application even after linking Qt Core.
I encountered an error message after including the QPermissions library in my project. I'm unsure why this is happening - is it possible that CMake is unable to locate the library in the Qt installation? Or perhaps the library is not included in the Qt package at all?
even wen I'm inluding the Qt Core library
cmake_minimum_required(VERSION 3.16) project(positioning VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOMOC ON) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.2 COMPONENTS Core Positioning Quick REQUIRED) qt_add_executable(apppositioning main.cpp ) qt_add_qml_module(apppositioning URI positioning VERSION 1.0 QML_FILES Main.qml ) set_target_properties(apppositioning PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_link_libraries(apppositioning PRIVATE Qt6::Core Qt6::Quick Qt6::Positioning ) install(TARGETS apppositioning BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
@Aninoss said in Need help getting user permission for location coordinates in Qt 6.5:
find_package(Qt6 6.2 COMPONENTS Core Positioning Quick REQUIRED)
You are at Qt 6.2 :)
Update to Qt 6.5 and you will findQPermissons
(or change your cmake, if you have multiple versions installed) -
@Aninoss said in Need help getting user permission for location coordinates in Qt 6.5:
find_package(Qt6 6.2 COMPONENTS Core Positioning Quick REQUIRED)
You are at Qt 6.2 :)
Update to Qt 6.5 and you will findQPermissons
(or change your cmake, if you have multiple versions installed)I'm sorry I should mention that I'm using Windows OS.
It's worth noting that Qt 6.5 has made some changes to the permission system to improve the framework's functionality. While the QPermission feature is only available for Android, iOS, and Web Assembly, Qt now relies on native OS APIs to obtain permissions.In the case of Windows, I find out the better way is by using the
namespace winrt
andnamespace Windows::Devices::Geolocation
to request permission from the user and access their device's location data. It's important to keep in mind that the accuracy of location information can vary depending on the source.
• GPS: within approximately 10 meters
• Wi-Fi: between approximately 30 meters and 500 meters
• Cell towers: between approximately 300 meters and 3,000 meters
• IP address: between approximately 1,000 meters and 5,000 metersThis code snippet utilizes the Windows native operating system to retrieve the user's location and display their coordinates. It requests and obtains the necessary permissions from the user before retrieving the location data.
#include "pch.h" #include <winrt/Windows.Devices.Geolocation.Geofencing.h> #include <iostream> #include <QCoreApplication> #include <QOperatingSystemVersion> #include <QGeoCoordinate> #include <QtPositioning/QGeoPositionInfoSource> #include <QtPositioning/QGeoAreaMonitorSource> #include <QtPositioning/QGeoSatelliteInfoSource> #include <QtPositioning/QGeoCoordinate> using namespace std; using namespace winrt; using namespace Windows::Foundation; using namespace Windows::Devices::Geolocation; int main(int argc, char *argv[]) { init_apartment(); QCoreApplication a(argc, argv); Geolocator geolocator; geolocator.DesiredAccuracy(PositionAccuracy::High); auto accessStatus = Geolocator::RequestAccessAsync().get(); if (accessStatus != GeolocationAccessStatus::Allowed) { std::cout << "Location access not granted" << std::endl; return 1; } auto location = geolocator.GetGeopositionAsync().get().Coordinate().Point().Position(); double latitude = location.Latitude; double longitude = location.Longitude; std::cout << "Latitude: " << latitude << std::endl; std::cout << "Longitude: " << longitude << std::endl; qDebug() << "Trying Position Info"; // Create a QGeoPositionInfoSource object and request location updates QGeoPositionInfoSource *posSource = QGeoPositionInfoSource::createDefaultSource(nullptr); if (posSource) { qDebug() << "Position Info created"; posSource->setUpdateInterval(10000); // Update the location every 1000 ms posSource->startUpdates(); // Start receiving location updates // Retrieve the latitude and longitude from the position information QObject::connect(posSource, &QGeoPositionInfoSource::positionUpdated, [](const QGeoPositionInfo info) { const qreal latitude = info.coordinate().latitude(); const qreal longitude = info.coordinate().longitude(); // QML UI Update Here }); } else { qDebug() << "Failed to create Position Info source"; } qDebug() << "Trying Area Monitor"; QGeoAreaMonitorSource *areaSource = QGeoAreaMonitorSource::createDefaultSource(nullptr); if (areaSource) { qDebug() << "Area Monitor created"; QObject::connect(areaSource, &QGeoAreaMonitorSource::errorOccurred, [](QGeoAreaMonitorSource::Error error) { qDebug() << "Satellite Info error occurred:" << error; }); } else { qDebug() << "Failed to create Area Monitor source"; } qDebug() << "Trying Satellite Info"; QGeoSatelliteInfoSource *satelliteSource = QGeoSatelliteInfoSource::createDefaultSource(nullptr); if (satelliteSource) { qDebug() << "Satellite Info created"; QObject::connect(satelliteSource, &QGeoSatelliteInfoSource::errorOccurred, [](QGeoSatelliteInfoSource::Error error) { qDebug() << "Satellite Info error occurred:" << error; }); } else { qDebug() << "Failed to create Satellite Info source"; } return a.exec(); }
-
A Aninoss has marked this topic as solved on