VXYModelMapper Example for chart



  • Hi,

    I wanna use a VXYModelMapper for my chart. I couldn't find a example.

    Where can I find a complete example?


  • Lifetime Qt Champion

    Hi,

    VXYModelMapper is part of QtChart which is a commercial only module. You should ask the Qt Company representatives for that.



  • Right now I'm evaluating the Qt enterprise version and I don't get technical support.

    I really stuck with this because there is no example to get this working...

    Thats my qml:
    @
    VXYModelMapper {
    model: myModel // QAbstractItemModel derived implementation
    series: spline
    }

    ChartView {
    anchors.fill: parent
    antialiasing: true
    backgroundColor: "#00000000"

    SplineSeries {
    id: spline
    }
    }
    @

    My model:

    @
    #include "timelinedetail.h"
    #include

    TimelineDetail::TimelineDetail(const int &kwh;, const int &time;)
    : m_kwh(kwh), m_time(time)
    {
    }

    int TimelineDetail::kwh() const
    {
    qDebug() << "m_kwh: " << m_kwh;
    return m_kwh;
    }

    int TimelineDetail::time() const
    {
    qDebug() << "m_time: " << m_time;
    return m_time;
    }

    TimelineDetailModel::TimelineDetailModel(QObject *parent)
    : QAbstractListModel(parent)
    {
    }

    void TimelineDetailModel::addDevice(const TimelineDetail &animal;)
    {
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_devices << animal;
    endInsertRows();
    }

    int TimelineDetailModel::rowCount(const QModelIndex & parent) const {
    Q_UNUSED(parent);
    return m_devices.count();
    }

    QVariant TimelineDetailModel::data(const QModelIndex & index, int role) const {
    if (index.row() < 0 || index.row() >= m_devices.count())
    return QVariant();

    const TimelineDetail &animal; = m_devices[index.row()];
    if (role == TypeRole)
    return animal.kwh();
    else if (role == SizeRole)
    qDebug() << "get time" << animal.time();
    return animal.time();
    return QVariant();
    }

    //![0]
    QHash TimelineDetailModel::roleNames() const {
    QHash roles;
    roles[TypeRole] = "kwh";
    roles[SizeRole] = "time";
    return roles;
    }
    @

    main.cpp:

    @
    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QtQml>
    #include <QModelIndex>
    #include <QQmlComponent>
    #include <QDebug>
    #include "timeline.h"
    #include "clientconnection.h"

    #include "timelinedetail.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    Timeline timeline;
    DataProvider dataProvider;
    ClientConnection clientConnection;
    
    //init
    dataProvider.setEngine(&engine);
    dataProvider.setTimeline(&timeline);
    clientConnection.setDataProvider(&dataProvider);
    timeline.setDefaultPeriod("day");
    timeline.setClientConnection(&clientConnection);
    timeline.setDataProvider(&dataProvider);
    timeline.setEngine(&engine);
    
    TimelineDetailModel model;
    model.addDevice(TimelineDetail(0, 0));
    model.addDevice(TimelineDetail(1, 3));
    model.addDevice(TimelineDetail(2, 2));
    
    
    
    
    
    engine.rootContext()->setContextProperty("myModel", &model);
    
    //set timeline context for QML
    engine.rootContext()->setContextProperty("timelineTest", &timeline);
    
    //set client context for QML
    engine.rootContext()->setContextProperty("clientConnection", &clientConnection);
    
    // instantiate the classes defined via the QML definition
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    
    return app.exec();
    

    }
    @



  • Is anyone out there who use qml charts with a c++ model?

    Thanks



  • I got a VBarModelMapper for BarSeries and QAbstractListModel running... but if I wanna use VXYModelMapper it doesn't work. My QAbstractListModel doesn't get called...

    Can anyone provide a quick sample how to use a VXYModelMapper with a SplineSeries.

    chart:

    @
    ChartView {
    anchors.fill: parent
    antialiasing: true

            SplineSeries {
                id: spline
    
                VXYModelMapper {
                    model: myModel // QAbstractItemModel derived implementation
    
                }
            }
        }
    

    @



  • I set up a quick example to keep it simple:

    @
    import QtQuick 2.3
    import QtQuick.Controls 1.2
    import QtQuick.Controls.Styles 1.2
    import QtQuick.Particles 2.0
    import QtQuick.Layouts 1.1
    import QtCharts 2.0

    Rectangle {
    anchors.fill: parent

    ListModel {
        id: testModel
    
        ListElement { x: 0; y: 10 }
        ListElement { x: 1; y: 11 }
        ListElement { x: 2; y: 14 }
        ListElement { x: 3; y: 10 }
        ListElement { x: 4; y: 11 }
        ListElement { x: 5; y: 17 }
        ListElement { x: 6; y: 14 }
        ListElement { x: 7; y: 13 }
        ListElement { x: 8; y: 10 }
    }
    
    ChartView {
        anchors.fill: parent
        antialiasing: true
    
        SplineSeries {
    
            VXYModelMapper {
                id: mapper
                model: testModel /*myModel*/ // QAbstractItemModel derived implementation
                //                series: spline
                //                    firstRow: 1
                xColumn: 0
                yColumn: 1
    
                Component.onCompleted: console.log("loaded VXYModelMapper: xColumn " + mapper.xColumn + " yColumn " + mapper.yColumn)
    
            }
        }
    }
    

    }
    @

    The example just works if I set the xColumn and yColumn property to 0. If I set the properties to 0 the Mapper just use the second values (in this example the y values) all time long...

    Any ideas?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.