无法从qml获取数据

问题描述 投票:0回答:1

我需要获取数据以便在 C++ 中进一步使用,但它给了我错误:

QObject::connect(QQuickWindow, QDeclarativeGeoMap):无效的 nullptr 参数

QObject::connect(QQuickWindow, QDeclarativeGeoMap):无效的 nullptr 参数

QGeoTileProviderOsm:在 QUrl 禁用 Tileserver(“http://maps-redirect.qt.io/osm/5.8/satellite”)

QGeoTileFetcherOsm:所有提供商均已解决

QGeoTileProviderOsm:在 QUrl 禁用 Tileserver(“http://maps-redirect.qt.io/osm/5.8/satellite”)

QGeoTileFetcherOsm:所有提供商均已解决

QT版本5.15.2

这是我的 QML 部分:

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtLocation 5.15
import QtPositioning 5.15
import QtQuick.Dialogs 1.3

Rectangle {
    id: telecom
    width: 800
    height: 600

    signal setRect(double x, double y, double width, double height)

    Plugin {
        id: mapPlugin
        name: "osm"

    }

    Map {
        id: map
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(55.7558, 37.6173)
        zoomLevel: 10

        MouseArea {
            id: mapMouseArea
            anchors.fill: parent
            onPressed: {
                mapMouseArea.active = true
            }
            onReleased: {
                mapMouseArea.active = false
            }
        }
    }

    Rectangle {
        id: rect
        width: 0
        height: 0
        color: "blue"
        opacity: 0.5
        border.color: "black"
        border.width: 2
        visible: false

        x: 100
        y: 100
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        onPressed: {
            rect.x = mouseArea.mouseX
            rect.y = mouseArea.mouseY
            rect.width = 0
            rect.height = 0
            rect.visible = true
        }

        onReleased: {
            rect.width = Math.abs(mouseArea.mouseX - rect.x)
            rect.height = Math.abs(mouseArea.mouseY - rect.y)
            setRect(rect.x, rect.y, rect.width, rect.height)
        }
...

main.cpp:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QQmlApplicationEngine engine;

    RectangleHandler rectangleHandler;
    engine.rootContext()->setContextProperty("rectangleHandler", &rectangleHandler);

    const QUrl url(QStringLiteral("qrc:/places_map.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &a, [url](QObject *obj, const QUrl &objUrl) {
                         if (!obj && url == objUrl) {
                             qCritical() << "Error 1";
                             QCoreApplication::exit(-1);
                         }
                     }, Qt::QueuedConnection);

    engine.load(url);
    if (engine.rootObjects().isEmpty()) {
        qCritical() << "Error 2";
        return -1;
    }

    MainWindow w;
    w.show();
    return a.exec();
}
c++ qt qml qt5.15
1个回答
0
投票

在 main.cpp 的以下行中,您将 <&a> 作为函数的参数传递。但你之前没有声明变量。我认为nullptr错误是由这个错误引起的。

QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &a, [url](QObject *obj, const QUrl &objUrl)

© www.soinside.com 2019 - 2024. All rights reserved.