Search code examples
c++qtgpsqtpositioning

QtPositioning QGeoPositionInfoSource::createSource returns nullptr for NMEA devices


I'm trying to follow along with the NMEA guide, and am having some trouble (Qt 6.7.0, MSVC 2022, Windows 11): https://doc.qt.io/qt-6/position-plugin-nmea.html

I have the Qt Positioning module installed, and can create a default GeoPositionInfoSource, but not an NMEA source. I'm instead getting a null pointer when calling the createSource function from the example. I've included an example.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)

project(qt-nmea-test LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Positioning)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Positioning)

add_executable(qt-nmea-test
  main.cpp
)
target_link_libraries(qt-nmea-test Qt${QT_VERSION_MAJOR}::Core
                                   Qt${QT_VERSION_MAJOR}::Positioning)

include(GNUInstallDirs)
install(TARGETS qt-nmea-test
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

main.cpp:

#include <QGeoPositionInfoSource>
#include <QVariantMap>

int main(int argc, char *argv[])
{
    qDebug() << "Available sources: " << QGeoPositionInfoSource::availableSources();

    QGeoPositionInfoSource *defaultSource = QGeoPositionInfoSource::createDefaultSource(nullptr);
    if (defaultSource != nullptr)
        qDebug() << "Default source is good!";
    else
        qDebug() << "Default source is null";

    QVariantMap params;
    params["nmea.source"] = "socket://localhost:22222";
    QGeoPositionInfoSource *nmeaSource = QGeoPositionInfoSource::createSource("nmea", params, nullptr);
    if (nmeaSource != nullptr)
        qDebug() << "NMEA source is good!";
    else
        qDebug() << "NMEA source is null";
}

I get the following output:

Available sources:  QList("nmea", "winrt")
Default source is good!
NMEA source is null

I've traced it to https://github.com/qt/qtpositioning/blob/v6.7.0/src/positioning/qgeopositioninfosource.cpp#L83. It's attempting to load the factory class, but the factory instance returned on this line is null.

I have similar results on Qt 6.6.2, using both MSVC 2022 and GCC 11 (Ubuntu 22.04).

Am I missing some initialization step? Am I using the plugin incorrectly?


Solution

  • The NMEA plugin also requires the Qt Serial Port plugin. After installing it, the same snippet of code works as expected.