QSignal in thread work for hight cpu
Unsolved
QML and Qt Quick
-
Hi all,
I have issue when draw chart in thread after a period of time then cpu up to 100% performent
can anyone help me?
Thanks,
qml fileimport 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: series2 axisX: axisX name: "From C++" color: "red" width: 5 } } }
main.cpp
#include <QApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include <thread> #include <QtCharts/QVXYModelMapper> #include "mydatamodel.h" #include <QtMath> void point_generator_proc(MyDataModel* model) { for(double t=0 ; ; t+=1) { double y = (1 + sin(t/10.0)) / 2.0; model->handleNewPoint(QPointF(t, y)); std::this_thread::sleep_for(std::chrono::milliseconds(50)); } } int main(int argc, char *argv[]) { QApplication app(argc, argv); QQmlApplicationEngine engine; auto context = engine.rootContext(); auto myDataModel = new MyDataModel(); auto 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"))); app.exec(); point_generator_thread.join(); return 0; }
mydatamodel.cpp
#include "mydatamodel.h" #include <QtMath> #include <QDebug> MyDataModel::MyDataModel() { QObject::connect(this, &MyDataModel::newPointAdded, this, &MyDataModel::addNewPoint, Qt::QueuedConnection); } void MyDataModel::handleNewPoint(const QPointF &point) { emit newPointAdded(point); } void MyDataModel::addNewPoint(const QPointF& point) { beginInsertRows(QModelIndex(), rowCount(), rowCount()); m_data.push_back(point); endInsertRows(); } 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()].x(); else return m_data[index.row()].y(); }
mydatamodel.h
#include <QAbstractTableModel> #include <QPointF> #include <vector> #include <QThread> class MyDataModel : public QAbstractTableModel { Q_OBJECT public: void handleNewPoint(const QPointF& point); signals: void newPointAdded(const QPointF& point); 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; private: void addNewPoint(const QPointF& point); std::vector<QPointF> m_data; };