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. VXYModelMapper Example for chart
Forum Updated to NodeBB v4.3 + New Features

VXYModelMapper Example for chart

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 3.0k 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.
  • M Offline
    M Offline
    morker
    wrote on last edited by
    #1

    Hi,

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

    Where can I find a complete example?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        morker
        wrote on last edited by
        #3

        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();
        

        }
        @

        1 Reply Last reply
        0
        • M Offline
          M Offline
          morker
          wrote on last edited by
          #4

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

          Thanks

          1 Reply Last reply
          0
          • M Offline
            M Offline
            morker
            wrote on last edited by
            #5

            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
            
                        }
                    }
                }
            

            @

            1 Reply Last reply
            0
            • M Offline
              M Offline
              morker
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0

              • Login

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