简体   繁体   中英

How to pass double from Qt c++ code to QML code?

I have this code in Qt

int main(int argc, char *argv[])  {
    QGuiApplication app(argc,argv);
       QQuickView view;
       QUrl q(QStringLiteral("QML:///places_map.qml"));
       view.setSource(QUrl(QStringLiteral("qrc:///places_map.qml")));
         //I've tried this to init qml properties
       QObject *object = view.rootObject();
         object->setProperty("latitude", 48.4656371);
         object->setProperty("longitude", 31.04900455);

         QQuickItem *item = view.rootObject();
        item->setProperty("device_latitude", 48.4656371);
        item->setProperty("device_longitude", 35.04900455);
}

And my qml file:

import QtQuick 2.0
import QtPositioning 5.5
import QtLocation 5.6

Rectangle {
    width: 720
    height: 480

    property double latitude:  0
    property double longitude: 0
    property double device_latitude: 0 //48.4656371
    property double device_longitude: 0 //35.54900455

    property variant location: QtPositioning.coordinate(latitude, longitude)
    property variant deviceLocation: QtPositioning.coordinate(device_latitude, device_longitude)

    Plugin {
        id: myPlugin
        name: "osm"
    }
    PlaceSearchModel {
           id: searchModel

           plugin: myPlugin

           searchTerm: "Pizza"
           searchArea: QtPositioning.circle(deviceLocation)

           Component.onCompleted: update()
       }
    Map {
        id: map
        anchors.fill: parent
        plugin: myPlugin;
        center: location
        zoomLevel: 13

        MapItemView {
            model: searchModel
            delegate: MapQuickItem {
                coordinate: deviceLocation //QtPositioning.coordinate(device_latitude, device_longitude)

                anchorPoint.x: image.width * 0.5
                anchorPoint.y: image.height

                sourceItem: Column {
                    Image { id: image; source: "marker.png" }
                    Text { text: title; font.bold: true }
                }
            }
        }
    }
}

In qml code properties double latitude and longitude set the view on map but map shows place with latitude = 0 and longtitude = 0

If I set the correct coordinates in qml code everything works How can I init this value from c++ code so that map will show my city ?

Using Q_PROPERTY would be better. Exposing Attributes of C++

I have it working like this:

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickItem>
#include <QQuickView>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQuickView view;
    view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));

    QQuickItem *item = view.rootObject();
    item->setProperty("number", 22.002);
    view.show();
    return app.exec();
}

main.qml

import QtQuick 2.4
import QtQuick.Window 2.2

Rectangle{
    visible: true
    width: 640
    height: 480
    property double number : 0

    Text {
        id: textEdit
        text: number
        verticalAlignment: Text.AlignVCenter
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.topMargin: 20
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM