From 78b21b5a29e4611d89d7d91be3d730a9e37456b4 Mon Sep 17 00:00:00 2001 From: isaac Date: Fri, 12 Aug 2016 00:39:15 +0430 Subject: [PATCH] Initial commit --- main.cpp | 43 +++++ main.qml | 47 +++++ mydatamodel.cpp | 51 +++++ mydatamodel.h | 18 ++ qtcharts_dynamic_series.pro | 12 ++ qtcharts_dynamic_series.pro.user | 318 +++++++++++++++++++++++++++++++ resources.qrc | 5 + 7 files changed, 494 insertions(+) create mode 100644 main.cpp create mode 100644 main.qml create mode 100644 mydatamodel.cpp create mode 100644 mydatamodel.h create mode 100644 qtcharts_dynamic_series.pro create mode 100644 qtcharts_dynamic_series.pro.user create mode 100644 resources.qrc diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..3b45014 --- /dev/null +++ b/main.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include + +#include "mydatamodel.h" + + +void point_generator_proc(MyDataModel* model) +{ + for(double t=0 ; ; t+=1) + { + double v = (1 + sin(t/10.0)) / 2.0; + + model->addNewPoint(std::make_pair(t, v)); + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } +} + + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + QQmlApplicationEngine engine; + auto context = engine.rootContext(); + + MyDataModel* myDataModel = new MyDataModel(); + + QtCharts::QVXYModelMapper* mapper = new QtCharts::QVXYModelMapper(); + mapper->setModel(myDataModel); + mapper->setXColumn(0); + mapper->setYColumn(1); + + std::thread point_generator_thread(point_generator_proc, myDataModel); + + context->setContextProperty("mapper", mapper); + engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); + + return app.exec(); +} diff --git a/main.qml b/main.qml new file mode 100644 index 0000000..140393a --- /dev/null +++ b/main.qml @@ -0,0 +1,47 @@ +import QtQuick 2.7 +import QtQuick.Window 2.2 +import QtQuick.Controls 2.0 +import QtCharts 2.0 + +ApplicationWindow { + visible: true + width: 600 + height: 300 + property int timeStep: 0 + + ChartView { + id: chartView + anchors.fill: parent + + ValueAxis { + id: axisX + min: 0 + max: 400 + } + + Component.onCompleted: { + mapper.series = series2 + } + + LineSeries { + id: series1 + axisX: axisX + } + + LineSeries { + id: series2 + axisX: axisX + } + } + + Timer { + interval: 100 + repeat: true + running: true + onTriggered: { + timeStep++; + var y = (1+Math.cos(timeStep/10.0))/2.0; + series1.append(timeStep, y); + } + } +} diff --git a/mydatamodel.cpp b/mydatamodel.cpp new file mode 100644 index 0000000..eadeb0d --- /dev/null +++ b/mydatamodel.cpp @@ -0,0 +1,51 @@ +#include "mydatamodel.h" + +MyDataModel::MyDataModel() +{ + +} + + +int MyDataModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return m_data.size(); +} + + +int MyDataModel::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return 2; +} + + +QVariant MyDataModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + Q_UNUSED(orientation) + Q_UNUSED(role) + + if(section == 0) + return "x"; + else + return "y"; +} + + +QVariant MyDataModel::data(const QModelIndex &index, int role) const +{ + Q_UNUSED(role) + + if (index.column() == 0) + return m_data[index.row()].first; + else + return m_data[index.row()].second; +} + + +void MyDataModel::addNewPoint(std::pair point) +{ + beginInsertRows(QModelIndex(), rowCount(), rowCount() + 1); + m_data.push_back(point); + endInsertRows(); +} diff --git a/mydatamodel.h b/mydatamodel.h new file mode 100644 index 0000000..55d09c5 --- /dev/null +++ b/mydatamodel.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include + +class MyDataModel : public QAbstractTableModel +{ + Q_OBJECT +public: + MyDataModel(); + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + + void addNewPoint(std::pair point); +private: + std::vector> m_data; +}; diff --git a/qtcharts_dynamic_series.pro b/qtcharts_dynamic_series.pro new file mode 100644 index 0000000..8e846c1 --- /dev/null +++ b/qtcharts_dynamic_series.pro @@ -0,0 +1,12 @@ +TEMPLATE = app + +QT += qml quick charts + +RESOURCES += resources.qrc + +SOURCES += main.cpp \ + mydatamodel.cpp +OTHER_FILES += *.qml + +HEADERS += \ + mydatamodel.h diff --git a/qtcharts_dynamic_series.pro.user b/qtcharts_dynamic_series.pro.user new file mode 100644 index 0000000..6821cec --- /dev/null +++ b/qtcharts_dynamic_series.pro.user @@ -0,0 +1,318 @@ + + + + + + EnvironmentId + {c2af9ce1-38ed-4636-a7c0-1e27c5614e8c} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.7.0 MSVC2015 32bit + Desktop Qt 5.7.0 MSVC2015 32bit + qt.57.win32_msvc2015_kit + 0 + 0 + 0 + + C:/Users/isaac/Desktop/New folder/build-qtcharts_dynamic_series-Desktop_Qt_5_7_0_MSVC2015_32bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + C:/Users/isaac/Desktop/New folder/build-qtcharts_dynamic_series-Desktop_Qt_5_7_0_MSVC2015_32bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + C:/Users/isaac/Desktop/New folder/build-qtcharts_dynamic_series-Desktop_Qt_5_7_0_MSVC2015_32bit-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + Deploy + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy locally + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + qtcharts_dynamic_series + + Qt4ProjectManager.Qt4RunConfiguration:C:/Users/isaac/Desktop/New folder/qtcharts_dynamic_series/qtcharts_dynamic_series.pro + true + + qtcharts_dynamic_series.pro + false + + + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 18 + + + Version + 18 + + diff --git a/resources.qrc b/resources.qrc new file mode 100644 index 0000000..5f6483a --- /dev/null +++ b/resources.qrc @@ -0,0 +1,5 @@ + + + main.qml + +