1

Qt のサンプルで利用可能なサンプル「qmlscatter」の修正版を開発しようとしています。私の意図は、「Data.qml」ファイルのデータ ポイントを変更して、3D 環境で飛行軌跡をプロットすることです。飛行経路の実際の座標は、3 つの異なる QVectors QVector<double>"cogPosX_"、"cogPosY_"、および "cogPosZ_" に格納され、各インデックスは後のタイム ステップを表します。

フォーラムで言及されているように、「.setContextProperty」関数を使用して QML ファイルのデータ値を更新する必要があります。しかし、それを機能させる方法を実際に見つけることはできません。主な理由は、正しい「dataModel」IDを参照していないことだと思います。ここに私のコードがあります:

datapointobject.h // これは、各データ ポイントの XY および Z 座標を含む汎用オブジェクトを定義する必要があります。

#ifndef DATAPOINTOBJECT_H
#define DATAPOINTOBJECT_H
#include <QWidget>
class DataObject : public QObject
{
    Q_OBJECT

    Q_PROPERTY(double x READ x WRITE setX)
    Q_PROPERTY(double y READ y WRITE setY)
    Q_PROPERTY(double z READ z WRITE setZ)
public:
    DataObject(QObject* parent =0);//Constructor
    DataObject(const double & x, const double & y, const double & z, QObject * parent=0);
    ~DataObject();//Destructor
    double x() const;
    double y() const;
    double z() const;
    void setX(const double &x);
    void setY(const double &y);
    void setZ(const double &z);
signals:

    void xChanged();
    void yChanged();
    void zChanged();
private:
    double m_x;
    double m_y;
    double m_z;
};
#endif // DATAPOINTOBJECT_H

datapointobject.cpp // これは、コンストラクターと関数を定義します。

#include "datapointobject.h"
#include<QDebug>

DataObject::DataObject(QObject *parent)//Constructor
    : QObject(parent)
{
}
DataObject::DataObject(const double &x, const double &y, const double &z, QObject *parent)//Constructor
    :QObject(parent), m_x(x), m_y(y), m_z(z)
{

}

DataObject::~DataObject(){//Destructor

}
double DataObject::x() const
{
    return m_x;
}
double DataObject::y() const
{
    return m_y;
}
double DataObject::z() const
{
    return m_z;
}
void DataObject::setX(const double &x){
    if (x != m_x) {
        m_x = x;
        emit xChanged();
    }
}
void DataObject::setY(const double &y){
    if (y != m_y) {
        m_y = y;
        emit yChanged();
    }
}
void DataObject::setZ(const double &z){
    if (z != m_z) {
        m_z = z;
        emit zChanged();
    }
}

threeDviewer.h // このヘッダーは、3D Scatter のプロットを担当する QQuickView インスタンスを作成する「Flightviewer」クラスを定義します。

#ifndef FLIGHTVIEWER_H
#define FLIGHTVIEWER_H
#include <QtWidgets>
#include <QtGui/QGuiApplication>
#include <QtCore/QDir>
#include <QtQuick/QQuickView>
#include <QtQml/QQmlEngine>
#include "flight.h"
#include <QtQml>

class Flightviewer: public QWidget
{
  Q_OBJECT

public:
  Flightviewer(Flight displayedFlight, QString directory, QWidget *parent = 0);//Constructor
  virtual ~Flightviewer();//Destructor
  QQuickView viewer;

  void showWindow();

  void readFlightTrajectory(Flight flight);
};


#endif // FLIGHTVIEWER_H

threeDviewer.cpp //このファイルはQQuickView viewerインスタンスを構成します。フライト データを QML データ ファイルにインポートする責任があります。

#include <QtGui/QGuiApplication>
#include <QtCore/QDir>
#include <QtQuick/QQuickView>
#include <QtQml/QQmlEngine>

#include "window.h"
#include <QWidget>
#include "threeDviewer.h"
#include "datapointobject.h"
Flightviewer::Flightviewer(Flight displayedFlight, QString directory, QWidget*parent){

    // The following are needed to make examples run without having to install the module
    // in desktop environments.
#ifdef Q_OS_WIN
    QString extraImportPath(QStringLiteral("%1/../../../../%2"));
#else
    QString extraImportPath(QStringLiteral("%1/../../../%2"));
#endif
    qmlRegisterType<DataObject>();
    readFlightTrajectory(displayedFlight);
    viewer.setVisible(false);//Open only after clicked.
    viewer.engine()->addImportPath(extraImportPath.arg(QGuiApplication::applicationDirPath(),
                                  QString::fromLatin1("qml")));
    //! [4]
    QObject::connect(viewer.engine(), &QQmlEngine::quit, &viewer, &QWindow::close);
    //! [4]

    viewer.setTitle(QStringLiteral("3D Flight Trajectory Visualization"));

    //! [3]
    viewer.setSource(QUrl("qrc:/qmlscatter/qml/qmlscatter/main.qml"));
    //! [3]

    viewer.setResizeMode(QQuickView::SizeRootObjectToView);

    //! [2]
    //! [2]
}
Flightviewer::~Flightviewer(){

}

void Flightviewer::showWindow(){
   viewer.showMaximized();
}
void Flightviewer::readFlightTrajectory(Flight flight){

   QList<QObject*> dataList;
   for (int i=0; i<flight.cogPosX_.size();i++){
       dataList.append(new DataObject(flight.cogPosX_.at(i),flight.cogPosY_.at(i),flight.cogPosZ_.at(i)));
   }
   QQmlContext * ctxt = viewer.rootContext();
   ctxt->setContextProperty("dataModel", QVariant::fromValue(dataList));
   viewer.update();
}

「QQuickViewer ビューアー」は、次のコマンドを使用して外部関数で初期化されます。

void Form::run3D(){
    threeDviewer= new Flightviewer(displayedFlights_[flightIndex],directory_);//initiate viewer

    threeDviewer->showWindow();//Show viewer

}

qml ファイルのデータは、「qmlscatter」の例とまったく同じように定義されています。

データ.qml:

import QtQuick 2.1
Item {

property alias model: dataModel

ListModel {
    id: dataModel
    //ListElement{ xPos: -1000.0; yPos: 500.0; zPos: -5.0 }
} 
}

データは、Scatter3D アイテムを定義する main.qml ファイルによってアクセスされます。

main.qml

    //! [0]
import QtQuick 2.1
import QtQuick.Layouts 1.0
import QtDataVisualization 1.0
import "."
//! [0]

//! [1]
Rectangle {
    id: mainView
    //! [1]
    width: 500
    height: 500

    //! [4]
    Data {
        id: seriesData
    }
    //! [4]

    //! [13]
    Theme3D {
        id: themeIsabelle
        type: Theme3D.ThemeIsabelle
        font.family: "Lucida Handwriting"
        font.pointSize: 40
    }
    //! [13]

    Theme3D {
        id: themeArmyBlue
        type: Theme3D.ThemeArmyBlue
    }

    //! [8]
    //! [9]
    Item {
        id: dataView
        anchors.bottom: parent.bottom
        //! [9]
        width: parent.width
        height: parent.height - buttonLayout.height
        //! [8]

        //! [2]
        Scatter3D {
            id: scatterGraph
            width: dataView.width
            height: dataView.height
            //! [2]
            //! [3]
            theme: themeIsabelle
            shadowQuality: AbstractGraph3D.ShadowQualitySoftLow
            //! [3]
            //! [6]
            axisX.segmentCount: 3
            axisX.subSegmentCount: 2
            axisX.labelFormat: "%.2f"
            axisZ.segmentCount: 2
            axisZ.subSegmentCount: 2
            axisZ.labelFormat: "%.2f"
            axisY.segmentCount: 2
            axisY.subSegmentCount: 2
            axisY.labelFormat: "%.2f"
            //! [6]
            //! [5]
            Scatter3DSeries {
                id: scatterSeries
                //! [5]
                //! [10]
                itemLabelFormat: "Series 1: X:@xLabel Y:@yLabel Z:@zLabel"
                //! [10]

                //! [11]
                ItemModelScatterDataProxy {
                    itemModel: seriesData.model
                    xPosRole: "xPos"
                    yPosRole: "yPos"
                    zPosRole: "zPos"
                }
                //! [11]
            }
        }
    }

    RowLayout {
        id: buttonLayout
        Layout.minimumHeight: cameraToggle.height
        width: parent.width
        anchors.left: parent.left
        spacing: 0
        //! [7]
        NewButton {
            id: shadowToggle
            Layout.fillHeight: true
            Layout.fillWidth: true
            text: scatterGraph.shadowsSupported ? "Hide Shadows" : "Shadows not supported"
            enabled: scatterGraph.shadowsSupported
            onClicked: {
                if (scatterGraph.shadowQuality === AbstractGraph3D.ShadowQualityNone) {
                    scatterGraph.shadowQuality = AbstractGraph3D.ShadowQualitySoftLow;
                    text = "Hide Shadows";
                } else {
                    scatterGraph.shadowQuality = AbstractGraph3D.ShadowQualityNone;
                    text = "Show Shadows";
                }
            }
        }
        //! [7]

        NewButton {
            id: smoothToggle
            Layout.fillHeight: true
            Layout.fillWidth: true
            text: "Use Smooth for Series One"
            onClicked: {
                if (scatterSeries.meshSmooth === false) {
                    text = "Use Flat for Series One";
                    scatterSeries.meshSmooth = true;
                } else {
                    text = "Use Smooth for Series One"
                    scatterSeries.meshSmooth = false;
                }
            }
        }

        NewButton {
            id: cameraToggle
            Layout.fillHeight: true
            Layout.fillWidth: true
            text: "Change Camera Placement"
            onClicked: {
                if (scatterGraph.scene.activeCamera.cameraPreset === Camera3D.CameraPresetFront) {
                    scatterGraph.scene.activeCamera.cameraPreset =
                            Camera3D.CameraPresetIsometricRightHigh;
                } else {
                    scatterGraph.scene.activeCamera.cameraPreset = Camera3D.CameraPresetFront;
                }
            }
        }

        NewButton {
            id: themeToggle
            Layout.fillHeight: true
            Layout.fillWidth: true
            text: "Change Theme"
            onClicked: {
                if (scatterGraph.theme.type === Theme3D.ThemeArmyBlue) {
                    scatterGraph.theme = themeIsabelle
                } else {
                    scatterGraph.theme = themeArmyBlue
                }
                if (scatterGraph.theme.backgroundEnabled === true) {
                    backgroundToggle.text = "Hide Background";
                } else {
                    backgroundToggle.text = "Show Background";
                }
            }
        }

        NewButton {
            id: backgroundToggle
            Layout.fillHeight: true
            Layout.fillWidth: true
            text: "Hide Background"
            onClicked: {
                if (scatterGraph.theme.backgroundEnabled === true) {
                    scatterGraph.theme.backgroundEnabled = false;
                    text = "Show Background";
                } else {
                    scatterGraph.theme.backgroundEnabled = true;
                    text = "Hide Background";
                }
            }
        }

        NewButton {
            id: exitButton
            Layout.fillHeight: true
            Layout.fillWidth: true
            text: "Quit"
            onClicked: Qt.quit(0);
        }
    }
}

これを適切にプログラムする方法のヒントを教えていただければ、非常にありがたいです。よろしくお願いします!

4

1 に答える 1