Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Need help getting user permission for location coordinates in Qt 6.5
Forum Updated to NodeBB v4.3 + New Features

Need help getting user permission for location coordinates in Qt 6.5

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 983 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AninossA Offline
    AninossA Offline
    Aninoss
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • AninossA Aninoss

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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();
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      AninossA 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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();
        }
        
        AninossA Offline
        AninossA Offline
        Aninoss
        wrote on last edited by
        #3

        @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?

        54546deb-2d8f-45fa-bd17-d05251e81b13-image.png

        1fee2e9a-fed2-4f7c-8b9b-98456a434fc0-image.png

        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})
        
        
        jsulmJ Pl45m4P 2 Replies Last reply
        0
        • AninossA Aninoss

          @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?

          54546deb-2d8f-45fa-bd17-d05251e81b13-image.png

          1fee2e9a-fed2-4f7c-8b9b-98456a434fc0-image.png

          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})
          
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Aninoss It's QPermission not QPermissions

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          AninossA 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Aninoss It's QPermission not QPermissions

            AninossA Offline
            AninossA Offline
            Aninoss
            wrote on last edited by Aninoss
            #5

            @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?"

            jsulmJ 1 Reply Last reply
            0
            • AninossA Aninoss

              @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?"

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Aninoss You're right, sorry! According to documentation it is indeed #include <QPermissions>
              Are you sure you're using Qt 6.5? That is the version where QPermissions was introduced.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • AninossA Aninoss

                @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?

                54546deb-2d8f-45fa-bd17-d05251e81b13-image.png

                1fee2e9a-fed2-4f7c-8b9b-98456a434fc0-image.png

                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})
                
                
                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #7

                @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 find QPermissons
                (or change your cmake, if you have multiple versions installed)

                • https://doc-snapshots.qt.io/qt6-dev/qlocationpermission.html
                • https://doc-snapshots.qt.io/qt6-6.5/qpermission.html

                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                AninossA 1 Reply Last reply
                1
                • Pl45m4P Pl45m4

                  @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 find QPermissons
                  (or change your cmake, if you have multiple versions installed)

                  • https://doc-snapshots.qt.io/qt6-dev/qlocationpermission.html
                  • https://doc-snapshots.qt.io/qt6-6.5/qpermission.html
                  AninossA Offline
                  AninossA Offline
                  Aninoss
                  wrote on last edited by
                  #8

                  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 and namespace 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 meters

                  This 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();
                  }
                  
                  1 Reply Last reply
                  0
                  • AninossA Aninoss has marked this topic as solved on

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved