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. FFT (in MATLAB) to C++ Model to QML

FFT (in MATLAB) to C++ Model to QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 439 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.
  • S Offline
    S Offline
    Sajjad Ali
    wrote on last edited by Sajjad Ali
    #1

    Hello, I want to take FFT in simulink and somehow make my C++ model get all that data. I will just get the data and put in constuctor just like I am doing with sine wave.

    How can I do this? or any alternative
    Everything found online was connect QML and Simulink.
    I want to Simulink -> C++ Model -> QML

    I have made a C++ model that looks like this:

    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
    
    
    #include "DataModel.h"
    #include "math.h"
    DataModel::DataModel(QObject *parent)
        : QAbstractTableModel{parent}
    {
        // code to produce sine
        for (int i = 0; i < 100; ++i)
            m_points.append(QPointF(i, qSin(i)*0.9));
        
    }
    
    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.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();
    }
    

    main.qml

    import QtQuick
    import QtCharts
    import hello.com as Peter
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
        ChartView {
            anchors.fill: parent
    
            LineSeries {
                id: lineSeries
                axisX: ValuesAxis {
                    min: 0
                    max: 10
                }
                axisY: ValuesAxis {
                    min: -1
                    max: 1
                }
                //useOpenGL: true
            }
    
            VXYModelMapper {
                xColumn: 0
                yColumn: 1
                firstRow: 0
                series: lineSeries
                model: Peter.MyData
            }
        }
    }
    
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SimonSchroeder
      wrote on last edited by
      #2

      I'm not an expert for QML, but I might help with some of your other questions. If you insist on using Matlab/Simulink, I have heard that there are so called mex files which will export your Matlab programs to be used from C/C++. This way you could try to export functionality from Matlab to be used by your program.

      I would rather suggest to look for an alternative. The goto library for FFT would be FFTW (https://www.fftw.org/).

      S 1 Reply Last reply
      0
      • S Offline
        S Offline
        SimonSchroeder
        wrote on last edited by
        #3

        BTW: I see you are using the QCharts module. This is only available under the GPL or commercial license, not LGPL. If you don't have the commercial license this automatically makes your software open source under the GPL. It is not a problem if you are using the software for yourself, but you always have to provide your source code to anybody who has the software. (If this is not your intention, there is Qwt for charts (though I don't think it supports QML directly.)

        1 Reply Last reply
        0
        • S SimonSchroeder

          I'm not an expert for QML, but I might help with some of your other questions. If you insist on using Matlab/Simulink, I have heard that there are so called mex files which will export your Matlab programs to be used from C/C++. This way you could try to export functionality from Matlab to be used by your program.

          I would rather suggest to look for an alternative. The goto library for FFT would be FFTW (https://www.fftw.org/).

          S Offline
          S Offline
          Sajjad Ali
          wrote on last edited by
          #4

          @SimonSchroeder I tried to export my MATLAB code as C++ files. And I got a lot of header and cpp files. Now I don't know how to put them in Qt.
          So, I'll use FFTW

          jsulmJ 1 Reply Last reply
          0
          • S Sajjad Ali

            @SimonSchroeder I tried to export my MATLAB code as C++ files. And I got a lot of header and cpp files. Now I don't know how to put them in Qt.
            So, I'll use FFTW

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Sajjad-Ali said in FFT (in MATLAB) to C++ Model to QML:

            Now I don't know how to put them in Qt

            You don't put this code in Qt.
            You either build it as part of your application or as a library and link your application with that library.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

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