从文本文件接收GPS数据并显示在QT Map中

时间:2019-05-24 02:39:21

标签: qt qml

我正在尝试使用UDP套接字连接来接收长而纬的文本文件数据,接收到的数据将显示在我的QT QML映射中。我可以在QT中这样做吗?

Plugin {
    id: mapPlugin
    name: "osm" 
   }

   MapQuickItem
   {
      id: marker
      anchorPoint.x: marker.width / 4
      anchorPoint.y: marker.height
      coordinate: QtPositioning.coordinate(13.293470, 135.816885)
      Image { source: "qrc:/marker.png"
             }
        Text { text: "Location" ;font.pointSize: 8; font.bold: true }

       }
    }

1 个答案:

答案 0 :(得分:0)

您必须实现一个位置为q_property的QObject,并使用setContextProperty(或使用qmlRegisterType)将其导出到QML,该QObject必须具有读取文本每一行并将其解析为位置的QGeoCoordinate设置的方法。以下代码是示例:

main.cpp

#include <QFile>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTextStream>
#include <QTimer>
#include <QGeoCoordinate>
#include <QRegularExpression>

class MarkerManager: public QObject
{
    Q_OBJECT
    Q_PROPERTY(QGeoCoordinate position READ position NOTIFY positionChanged)
public:
    MarkerManager(QObject *parent=nullptr): QObject(parent){
        connect(&timer, &QTimer::timeout, this, &MarkerManager::readLine);
    }
    bool loadFile(const QString & filename, int interval=100){
        file.setFileName(filename);
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return false;
        ts.setDevice(&file);
        timer.start(interval);
        return true;
    }
    QGeoCoordinate position() const{
        return m_position;
    }
Q_SIGNALS:
    void positionChanged();
private Q_SLOTS:
    void readLine(){
        if(!ts.atEnd()){
            QString line = ts.readLine();
            QStringList elements = line.split(QRegularExpression("\\s+"));
            if(elements.count() == 2){
                bool ok1, ok2;
                double lat = elements[0].toDouble(&ok1);
                double lng = elements[1].toDouble(&ok2);
                if(ok1 && ok2){
                    m_position = QGeoCoordinate(lat, lng);
                    Q_EMIT positionChanged();
                }
            }
        }
    }
private:
    QFile file;
    QTextStream ts;
    QTimer timer;
    QGeoCoordinate m_position;
};

#include "main.moc"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);
    MarkerManager manager;
    manager.loadFile("data_gps.txt");
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("manager", &manager);
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

main.qml

import QtQuick 2.12
import QtQuick.Window 2.12

import QtLocation 5.12
import QtPositioning 5.12

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Plugin {
        id: mapPlugin
        name: "osm"
    }
    Map {
        id: map
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(14.70202102, 121.0957246)
        zoomLevel: 15
        MapQuickItem{
            id: marker
            anchorPoint.x: marker.width / 4
            anchorPoint.y: marker.height
            coordinate: manager.position
            sourceItem:  Image { source: "qrc:/marker.png"
                Text { text: "Location" ; font.pointSize: 8; font.bold: true }
            }
        }
    }
}

完整示例见here