Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. C++ Model to QML ChartView - Not Showing Anything
Qt 6.11 is out! See what's new in the release blog

C++ Model to QML ChartView - Not Showing Anything

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 451 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sajjad Ali
    wrote on last edited by
    #1

    Hello, I tried to make a C++ model. Then put it to ChartView in QML. Graph is not showing anything. I can't find anything related example online. Pls help me identify what I am doing wrong.

    main.cpp

    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include "DataModel.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QQmlApplicationEngine engine;
    
        DataModel MyModel;
        qmlRegisterSingletonInstance ("hello.com",1,0,"MyData",&MyModel);
    
        const QUrl url(u"qrc:/CPPModelForChart/main.qml"_qs);
        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();
    }
    
    

    DataModel.h

    #ifndef DATAMODEL_H
    #define DATAMODEL_H
    
    #include <QObject>
    #include <QAbstractTableModel>
    #include <QList>
    #include <QPointF>
    
    class DataModel : public QAbstractTableModel
    {
        Q_OBJECT
    public:
        enum {
            ValueRole = Qt::DisplayRole
        };
        explicit DataModel(QObject *parent = nullptr);
        int rowCount(const QModelIndex &parent) const;
        int columnCount(const QModelIndex &parent) const;
        QVariant data(const QModelIndex &index, int role) const;
        QHash<int, QByteArray> roleNames() const;
    
    signals:
    
    private:
        QList <QPointF> m_points;
    };
    
    #endif // DATAMODEL_H
    
    

    DataModel.cpp

    #include "DataModel.h"
    
    DataModel::DataModel(QObject *parent)
        : QAbstractTableModel{parent}
    {
        m_points << QPointF(0, 0) << QPointF(1, 1) << QPointF(2, 4);
    }
    
    int DataModel::rowCount(const QModelIndex &parent) const
    {
        return m_points.size();
    }
    
    int DataModel::columnCount(const QModelIndex &parent) const
    {
        return 2;
    }
    
    QVariant DataModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid() || role != Qt::DisplayRole)
            return QVariant();
    
        const QPointF p = m_points[index.row()];
    
        if (index.column() == 0)
            return p.x();
    
        return p.y();
    }
    
    QHash<int, QByteArray> DataModel::roleNames() const
    {
        QHash<int, QByteArray> mappings;
        mappings [ValueRole] = "value";
        return mappings;
    }
    
    

    main.qml

    import QtQuick
    import QtCharts
    import hello.com as Peter
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
        // Write chartview that can take model from c++
        ChartView {
            id: dataChartView
            animationOptions: ChartView.NoAnimation
            antialiasing: true
            anchors.fill: parent
    
            ValuesAxis {
                id: axisXdata
                min: 0
                max: 3
            }
    
            ValuesAxis {
                id: axisYdata
                min: 0
                max: 5
            }
    
            LineSeries {
                id: dataLineSeries
                name: "Data"
                axisX: axisXdata
                axisY: axisYdata
                useOpenGL: true
            }
    
            VXYModelMapper {
                id: dataModelMapper
                model: Peter.MyData
                series: dataLineSeries
                firstRow: 0
                xColumn: 0
                yColumn: 1
            }
        }
    }
    
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sajjad Ali
      wrote on last edited by
      #2

      Update: I don't know why. It works when I remove OpenGL stuff from QML file.

      1 Reply Last reply
      0
      • S Sajjad Ali has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved