2017-04-06 76 views
0

我有這樣的代碼在Qt的如何從Qt C++代碼傳遞double到QML代碼?

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

我的QML文件:

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 } 
       } 
      } 
     } 
    } 
} 

在QML代碼性能的雙緯度和經度設置視圖地圖 但地圖上顯示place with latitude = 0 and longtitude = 0

如果我在qml代碼中設置了正確的座標一切正常 如何從C++代碼初始化該值,以便地圖顯示我的城市?

回答

0

使用Q_PROPERTY會更好。 Exposing Attributes of C++

我有它的工作是這樣的:

的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 
    } 
} 
+0

感謝您的回答, 緯度和經度現在工作以及與以前的C++代碼 我有三編輯你的代碼以獲取其他數據(device_latitute和device_longtitude),但它不起作用,請你看看我更新後的代碼? – Dmitry

+0

你缺少#包含嗎?看到我更新的答案。 –