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: how to define x and y values

VXYModelMapper: how to define x and y values

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlc++chartsmodels
2 Posts 2 Posters 1.3k Views
  • 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.
  • C Offline
    C Offline
    corianderop
    wrote on 26 Jul 2016, 20:09 last edited by corianderop
    #1

    I have a model which I've exposed to qml that has two role and creates two columns. I'm trying to graph the content of these two columns by using the VXYModelMapper class and I'm running into some trouble. It's not really clear to me from the documentation how to use the xColumn and yColumn to define my x and y data. What I've tried so far is something like:

    cmodel.cpp

    using namespace std;
    cmodel::cmodel()
        : xVector(),yVector()
    {
        xVector.append(0);
        xVector.append(1);
        xVector.append(2);
        xVector.append(3);
    
        yVector.append(0);
        yVector.append(1);
        yVector.append(2);
        yVector.append(3);
    }
    int cmodel::rowCount( const QModelIndex& parent) const
    {
        return 4;
    }
    
    QHash<int, QByteArray> cmodel::roleNames() const {
        QHash<int, QByteArray> roles;
        roles[X_ROLE] = "xvalue";
        roles[Y_ROLE] ="yvalue";
    
        return roles;
    }
    bool cmodel::setData(const QModelIndex &index, const QVariant &value, int role){
        return true;
    }
    
    bool cmodel::insertColumns(int position, int columns, const QModelIndex &parent)
    {
    
        return true;
    }
    
    bool cmodel::removeColumns(int position, int columns, const QModelIndex &parent)
    {
    
        return true;
    }
    
    QVariant cmodel::data(const QModelIndex& index, int role) const {
        if( index.row() >= rowCount()){
            return QVariant::Invalid;
        }
        else{
            QVariant result;
            switch(role){
            case X_ROLE:
                result=xVector[index.row()];
                cout<<"X_ROLE: "<<X_ROLE<<endl;
                break;
            case Y_ROLE:
                result=yVector[index.row()];
                cout<<"Y_ROLE: "<<Y_ROLE<<endl;
                break;
    
            }//switch
            return result;
        }//else
    }//QVariant
    

    main.qml

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.1
    import QtQuick.Controls.Styles 1.4
    import QtQuick.Window 2.2
    import QtCharts 2.0
    ApplicationWindow {
        width: Screen.width/3;
        height: Screen.height/3
        visible: true
    
     //Item{
        //anchors.bottom: parent.bottom
        ChartView {
            //title:"Line"
            anchors.fill:parent
            antialiasing: true
    
    
            LineSeries{
                VXYModelMapper{
                model:myGraph
                xColumn:257
                yColumn:258
                }
            }//lineseries
    
    
        }//chartview
     //}
    Item{
        anchors.left:parent.left
        TableView{
        id: graphTableView
        //anchors.fill: parent
        model:myGraph
    
    
    
        TableViewColumn{
            role:"xvalue"
            title:"X_ROLE"
            width:100
        }
        TableViewColumn{
            role:"yvalue"
            title:"Y_ROLE"
            width:100
        }
    
        }//tableview
    }
    }//applicationWindow
    

    In my case X_ROLE and Y_ROLE are 257 and 258 respectively which is why I hardcoded them in the x/yColumn as a test. This isn't working. I'm confused as to whether x/yColumn are roles or something else. I haven't been able to find any examples on how to use this class.

    When I use a tableview to simply list the contents of the two column I get what I want but I'm stuck on graphing it.

    Has anyone implemented this and have any hints?

    [Added code formatting tags ~kshegunov]

    S 1 Reply Last reply 17 Mar 2017, 13:28
    0
    • C corianderop
      26 Jul 2016, 20:09

      I have a model which I've exposed to qml that has two role and creates two columns. I'm trying to graph the content of these two columns by using the VXYModelMapper class and I'm running into some trouble. It's not really clear to me from the documentation how to use the xColumn and yColumn to define my x and y data. What I've tried so far is something like:

      cmodel.cpp

      using namespace std;
      cmodel::cmodel()
          : xVector(),yVector()
      {
          xVector.append(0);
          xVector.append(1);
          xVector.append(2);
          xVector.append(3);
      
          yVector.append(0);
          yVector.append(1);
          yVector.append(2);
          yVector.append(3);
      }
      int cmodel::rowCount( const QModelIndex& parent) const
      {
          return 4;
      }
      
      QHash<int, QByteArray> cmodel::roleNames() const {
          QHash<int, QByteArray> roles;
          roles[X_ROLE] = "xvalue";
          roles[Y_ROLE] ="yvalue";
      
          return roles;
      }
      bool cmodel::setData(const QModelIndex &index, const QVariant &value, int role){
          return true;
      }
      
      bool cmodel::insertColumns(int position, int columns, const QModelIndex &parent)
      {
      
          return true;
      }
      
      bool cmodel::removeColumns(int position, int columns, const QModelIndex &parent)
      {
      
          return true;
      }
      
      QVariant cmodel::data(const QModelIndex& index, int role) const {
          if( index.row() >= rowCount()){
              return QVariant::Invalid;
          }
          else{
              QVariant result;
              switch(role){
              case X_ROLE:
                  result=xVector[index.row()];
                  cout<<"X_ROLE: "<<X_ROLE<<endl;
                  break;
              case Y_ROLE:
                  result=yVector[index.row()];
                  cout<<"Y_ROLE: "<<Y_ROLE<<endl;
                  break;
      
              }//switch
              return result;
          }//else
      }//QVariant
      

      main.qml

      import QtQuick 2.5
      import QtQuick.Controls 1.4
      import QtQuick.Layouts 1.1
      import QtQuick.Controls.Styles 1.4
      import QtQuick.Window 2.2
      import QtCharts 2.0
      ApplicationWindow {
          width: Screen.width/3;
          height: Screen.height/3
          visible: true
      
       //Item{
          //anchors.bottom: parent.bottom
          ChartView {
              //title:"Line"
              anchors.fill:parent
              antialiasing: true
      
      
              LineSeries{
                  VXYModelMapper{
                  model:myGraph
                  xColumn:257
                  yColumn:258
                  }
              }//lineseries
      
      
          }//chartview
       //}
      Item{
          anchors.left:parent.left
          TableView{
          id: graphTableView
          //anchors.fill: parent
          model:myGraph
      
      
      
          TableViewColumn{
              role:"xvalue"
              title:"X_ROLE"
              width:100
          }
          TableViewColumn{
              role:"yvalue"
              title:"Y_ROLE"
              width:100
          }
      
          }//tableview
      }
      }//applicationWindow
      

      In my case X_ROLE and Y_ROLE are 257 and 258 respectively which is why I hardcoded them in the x/yColumn as a test. This isn't working. I'm confused as to whether x/yColumn are roles or something else. I haven't been able to find any examples on how to use this class.

      When I use a tableview to simply list the contents of the two column I get what I want but I'm stuck on graphing it.

      Has anyone implemented this and have any hints?

      [Added code formatting tags ~kshegunov]

      S Offline
      S Offline
      SaranshVora
      wrote on 17 Mar 2017, 13:28 last edited by
      #2

      @corianderop Hi I am also stuck in a similar situation did you figure out a solution for it ?

      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