-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 78b21b5
Showing
7 changed files
with
494 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <QApplication> | ||
#include <QQmlApplicationEngine> | ||
#include <QQmlContext> | ||
#include <thread> | ||
#include <QtCharts/QVXYModelMapper> | ||
|
||
#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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<double, double> point) | ||
{ | ||
beginInsertRows(QModelIndex(), rowCount(), rowCount() + 1); | ||
m_data.push_back(point); | ||
endInsertRows(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
#include <QAbstractTableModel> | ||
#include <vector> | ||
|
||
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<double, double> point); | ||
private: | ||
std::vector<std::pair<double,double>> m_data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
TEMPLATE = app | ||
|
||
QT += qml quick charts | ||
|
||
RESOURCES += resources.qrc | ||
|
||
SOURCES += main.cpp \ | ||
mydatamodel.cpp | ||
OTHER_FILES += *.qml | ||
|
||
HEADERS += \ | ||
mydatamodel.h |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<RCC> | ||
<qresource prefix="/"> | ||
<file>main.qml</file> | ||
</qresource> | ||
</RCC> |