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. Get QtGraphs with XYModelMapper working
Qt 6.11 is out! See what's new in the release blog

Get QtGraphs with XYModelMapper working

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 2 Posters 164 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.
  • I Offline
    I Offline
    Igor23
    wrote last edited by
    #1

    Hallo,

    I'm trying to get QtGraphs working with a model. But I only get a grey window:

    The qml file:

    import QtQuick
    import QtGraphs
    import graphsexample
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
        GraphsView {
            antialiasing: true
            anchors.fill: parent
            //legend.visible: false
            LineSeries {
                XYModelMapper {
                    model: Graphsmodel
                    xSection: 0
                    ySection: 1
                }
            }
        }
    }
    

    And the model cpp files:

    #ifndef GRAPHSMODEL_H
    #define GRAPHSMODEL_H
    
    #include <cmath>
    #include <QObject>
    #include <QAbstractListModel>
    
    #define PI 3.14159265359
    
    class GraphsModel : public QAbstractTableModel
    {
       Q_OBJECT
    
    public:
        explicit GraphsModel();
    
        virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
        virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;
    
        QVariant data(const QModelIndex &index, int role) const;
        virtual void populate();
    
    protected:
        int ncoords;
        std::vector<double> xvalues;
        std::vector<double> yvalues;
        QList< std::pair<double,double> > mDatas;
    };
    
    #endif // GRAPHSMODEL_H
    

    and

    #include "graphsmodel.h"
    
    
    GraphsModel::GraphsModel() : QAbstractTableModel()
    {
        int icoords;
        ncoords= 100;
        xvalues= std::vector<double>(ncoords);
        yvalues= std::vector<double>(ncoords);
    
        for(icoords=0; icoords<ncoords; icoords++){
           xvalues[icoords]= double(5*icoords)*PI / 180.0;
           yvalues[icoords]= std::sin(xvalues[icoords]);
        }
    
        mDatas= QList< std::pair<double, double> >(ncoords);
    
        populate();
    
        return;
    }
    
    int GraphsModel::rowCount(const QModelIndex& parent) const
    {
          return ncoords;
    }
    
    int GraphsModel::columnCount(const QModelIndex& parent) const
    {
          return 2;
    }
    
    QVariant GraphsModel::data(const QModelIndex& index, int role) const
    {
        int row = index.row();
        int column= index.column();
    
        if(row < 0 || row >= ncoords) {
            return 0;
        }
    
        if(column == 0) {
                return QVariant(std::get<0>(mDatas.at(row)));
        }
        else if(column == 1) {
                return QVariant(std::get<1>(mDatas.at(row)));
        }
        else return QVariant(0);
    }
    
    
    void GraphsModel::populate()
    {
        int icoord;
        mDatas=QList< std::pair<double,double> >(ncoords);
    
        for(icoord=0; icoord<ncoords; icoord++){
            mDatas[icoord]= std::make_pair(xvalues[icoord], yvalues[icoord]);
        }
    
        emit dataChanged(index(0,0),index(ncoords-1,1));
    
        return;
    }
    

    The main.cpp file is:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    #include "graphsmodel.h"
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        GraphsModel graphsmodel;
        qmlRegisterSingletonInstance("graphsexample", 1, 0, "Graphsmodel", &graphsmodel);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/codeExampleQtGraphs/Main.qml")));
    
        return app.exec();
    }
    

    I hope anyone can help me. I think its only small error ...

    1 Reply Last reply
    0
    • GrecKoG Online
      GrecKoG Online
      GrecKo
      Qt Champions 2018
      wrote last edited by
      #2

      You need to set the series property of your XYModelMapper.

      I 2 Replies Last reply
      1
      • GrecKoG GrecKo

        You need to set the series property of your XYModelMapper.

        I Offline
        I Offline
        Igor23
        wrote last edited by
        #3

        @GrecKo Thanks! I also found that out. But the lineseries must also have a color different from backround and there must be explicit axes. My working qml file is now:

        import QtQuick
        import QtGraphs
        import graphsexample 1.0
        
        Window {
            width: 640
            height: 480
            visible: true
            title: qsTr("Hello World")
            GraphsView {
                id: graphsView
                antialiasing: true
                anchors.fill: parent
                //legend.visible: false
                ValueAxis {
                    id: axisX
                    min: 0
                    max: 10  // Anpassen an Ihre Daten
                    titleText: "X-Achse"
                }
        
                ValueAxis {
                    id: axisY
                    min: 0
                    max: 10  // Anpassen an Ihre Daten
                    titleText: "Y-Achse"
                }
                LineSeries {
                    id: lineSeries
                    color: "#00ff00"
                    joinStyle: Qt.RoundJoin
                    XYModelMapper {
                        model: Graphsmodel
                        series: lineSeries
                        xSection: 0
                        ySection: 1
                    }
                    axisX: axisX
                    axisY: axisY
                }
            }
        }
        

        Also this Connections don't work. It must be Graphsmodel with captal letter in XYModelMapper:

            Connections{
                id: graphsmodel
                target: Graphsmodel
            }
        
        1 Reply Last reply
        0
        • GrecKoG GrecKo

          You need to set the series property of your XYModelMapper.

          I Offline
          I Offline
          Igor23
          wrote last edited by
          #4

          @GrecKo I have an additional question, because it is important for my project:
          How can I update the axes in the following sense:

          ValueAxis {
              id: axisX
              min: Graphsmodel.returnXmin()
              max: Graphsmodel.returnXmax()
              titleText: "X-Achse"
          }
          

          Even with fixed starting values this is not working for me.

          1 Reply Last reply
          0
          • GrecKoG Online
            GrecKoG Online
            GrecKo
            Qt Champions 2018
            wrote last edited by
            #5

            Try to expose the xMin and xMan as properties with a NOTIFY signal instead to make the min and max binding automatically update. I guess it is currently calling returnXmin/max initially when the model is maybe empty, and then it isn't called again.

            I 1 Reply Last reply
            1
            • GrecKoG GrecKo

              Try to expose the xMin and xMan as properties with a NOTIFY signal instead to make the min and max binding automatically update. I guess it is currently calling returnXmin/max initially when the model is maybe empty, and then it isn't called again.

              I Offline
              I Offline
              Igor23
              wrote last edited by Igor23
              #6

              @GrecKo How do you mean this exactly? Can you give an example code?

              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