IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Qt Quick Discussion :

Graphique en temps réel avec QCustomPlot


Sujet :

Qt Quick

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2022
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2022
    Messages : 3
    Points : 2
    Points
    2
    Par défaut Graphique en temps réel avec QCustomPlot
    Bonjour, je suis débutant en C++ et Qt et je voulais tracer, a l'aide de la librairie QCustomPlot un graphe en fonction du temps et l'afficher dans une fenêtre QML.
    J'ai suivi l'exemple donné sur leur site (https://www.qcustomplot.com/index.ph...altimedatademo) puis j'ai essayé de l'intégrer dans un QML en utilisant les codes créé ici https://www.qcustomplot.com/index.php/support/forum/172

    Mon problème est que lors de l'affichage de ma fenêtre, rien n'est tracé sur mon plot.

    customploitem.h
    Code Qt : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    #pragma once
     
    #include <QtQuick>
    #include "../../qcustomplot.h"
     
    class QCustomPlot;
     
    class CustomPlotItem : public QQuickPaintedItem
    {
        Q_OBJECT
     
    public:
        CustomPlotItem( QQuickItem* parent = 0 );
        virtual ~CustomPlotItem();
     
        void paint( QPainter* painter );
     
        Q_INVOKABLE void initCustomPlot();
        void setupPlot(QCustomPlot* customPlot );
     
    protected:
        void routeMouseEvents( QMouseEvent* event );
     
        virtual void mousePressEvent( QMouseEvent* event );
        virtual void mouseReleaseEvent( QMouseEvent* event );
        virtual void mouseMoveEvent( QMouseEvent* event );
        virtual void mouseDoubleClickEvent( QMouseEvent* event );
     
        void setupQuadraticDemo( QCustomPlot* customPlot );
     
    private:
        QCustomPlot*            m_CustomPlot;
        QTimer*                 dataTimer;
     
    private slots:
        void graphClicked(QCPAbstractPlottable* plottable);
        void onCustomReplot();
        void updateCustomPlotSize();
        void realtimeDataSlot(QCustomPlot* customPlot);
     
    };

    customplotitem.cpp
    Code Qt : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    #include "CustomPlotItem.h"
    #include "../../qcustomplot.h"
    #include <QDebug>
     
    CustomPlotItem::CustomPlotItem( QQuickItem* parent ) : QQuickPaintedItem( parent ), m_CustomPlot( nullptr )
    {
        setFlag( QQuickItem::ItemHasContents, true );
        // setRenderTarget(QQuickPaintedItem::FramebufferObject);
        // setAcceptHoverEvents(true);
        setAcceptedMouseButtons( Qt::AllButtons );
     
        connect( this, &QQuickPaintedItem::widthChanged, this, &CustomPlotItem::updateCustomPlotSize );
        connect( this, &QQuickPaintedItem::heightChanged, this, &CustomPlotItem::updateCustomPlotSize );
    }
     
    CustomPlotItem::~CustomPlotItem()
    {
        delete m_CustomPlot;
        m_CustomPlot = nullptr;
    }
     
    void CustomPlotItem::initCustomPlot()
    {
        m_CustomPlot = new QCustomPlot();
     
        updateCustomPlotSize();
     
        setupPlot(m_CustomPlot);
        realtimeDataSlot(m_CustomPlot);
        //setupQuadraticDemo(m_CustomPlot);
     
        connect( m_CustomPlot, &QCustomPlot::afterReplot, this, &CustomPlotItem::onCustomReplot );
        m_CustomPlot->replot();
    }
     
    void CustomPlotItem::setupPlot(QCustomPlot* customPlot)
    {
        customPlot->addGraph();
        customPlot->graph(0)->setPen(QPen(QColor(40, 110, 255)));
     
        QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
        timeTicker->setTimeFormat("%h:%m:%s");
        customPlot->xAxis->setTicker(timeTicker);
        customPlot->axisRect()->setupFullAxesBox();
        customPlot->yAxis->setRange(-1.2, 1.2);
     
        // make left and bottom axes transfer their ranges to right and top axes:
        connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
        connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
     
        // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
        dataTimer = new QTimer(this);
        connect(dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot(m_customPlot)));
        dataTimer->start(10);
    }
     
    void CustomPlotItem::realtimeDataSlot(QCustomPlot* customPlot)
    {
        static QTime time(QTime::currentTime());
        // calculate two new data points:
        double key = time.elapsed()/1000.0; // time elapsed since start of demo, in seconds
        static double lastPointKey = 0;
        if (key-lastPointKey > 0.02) // at most add point every 20 ms
        {
          // add data to lines:
          customPlot->graph(0)->addData(key, qSin(key)+qrand()/(double)RAND_MAX*1*qSin(key/0.3843));
          //ui->customPlot->graph(1)->addData(key, qCos(key)+qrand()/(double)RAND_MAX*0.5*qSin(key/0.4364));
          // rescale value (vertical) axis to fit the current data:
          customPlot->graph(0)->rescaleValueAxis();
          //ui->customPlot->graph(1)->rescaleValueAxis(true);
          lastPointKey = key;
        }
        // make key axis range scroll with the data (at a constant range size of 8):
        customPlot->xAxis->setRange(key, 8, Qt::AlignRight);
        customPlot->replot();
    }
     
    void CustomPlotItem::paint( QPainter* painter )
    {
        if (m_CustomPlot)
        {
            QPixmap    picture( boundingRect().size().toSize() );
            QCPPainter qcpPainter( &picture );
     
            //m_CustomPlot->replot();
            m_CustomPlot->toPainter( &qcpPainter );
     
            painter->drawPixmap( QPoint(), picture );
        }
    }
     
    void CustomPlotItem::mousePressEvent( QMouseEvent* event )
    {
        qDebug() << Q_FUNC_INFO;
        routeMouseEvents( event );
    }
     
    void CustomPlotItem::mouseReleaseEvent( QMouseEvent* event )
    {
        qDebug() << Q_FUNC_INFO;
        routeMouseEvents( event );
    }
     
    void CustomPlotItem::mouseMoveEvent( QMouseEvent* event )
    {
        routeMouseEvents( event );
    }
     
    void CustomPlotItem::mouseDoubleClickEvent( QMouseEvent* event )
    {
        qDebug() << Q_FUNC_INFO;
        routeMouseEvents( event );
    }
     
    void CustomPlotItem::graphClicked( QCPAbstractPlottable* plottable )
    {
        qDebug() << Q_FUNC_INFO << QString( "Clicked on graph '%1 " ).arg( plottable->name() );
    }
     
    void CustomPlotItem::routeMouseEvents( QMouseEvent* event )
    {
        if (m_CustomPlot)
        {
            QMouseEvent* newEvent = new QMouseEvent( event->type(), event->localPos(), event->button(), event->buttons(), event->modifiers() );
            //QCoreApplication::sendEvent( m_CustomPlot, newEvent );
            QCoreApplication::postEvent( m_CustomPlot, newEvent );
        }
    }
     
    void CustomPlotItem::updateCustomPlotSize()
    {
        if (m_CustomPlot)
        {
            m_CustomPlot->setGeometry( 0, 0, width(), height() );
        }
    }
     
    void CustomPlotItem::onCustomReplot()
    {
        qDebug() << Q_FUNC_INFO;
        update();
    }
     
    void CustomPlotItem::setupQuadraticDemo( QCustomPlot* customPlot )
    {
        // make top right axes clones of bottom left axes:
        QCPAxisRect* axisRect = customPlot->axisRect();
     
        // generate some data:
        QVector<double> x( 101 ), y( 101 );   // initialize with entries 0..100
        QVector<double> lx( 101 ), ly( 101 ); // initialize with entries 0..100
        for (int i = 0; i < 101; ++i)
        {
            x[i] = i / 50.0 - 1;              // x goes from -1 to 1
            y[i] = x[i] * x[i];               // let's plot a quadratic function
     
            lx[i] = i / 50.0 - 1;             //
            ly[i] = lx[i];                    // linear
        }
        // create graph and assign data to it:
        customPlot->addGraph();
        customPlot->graph( 0 )->setPen( QPen( Qt::red ) );
        customPlot->graph( 0 )->setPen( QPen( Qt::blue, 2 ) );
        customPlot->graph( 0 )->setData( x, y );
     
        customPlot->addGraph();
        customPlot->graph( 1 )->setPen( QPen( Qt::magenta ) );
        customPlot->graph( 1 )->setPen( QPen( Qt::blue, 2 ) );
        customPlot->graph( 1 )->setData( lx, ly );
     
        // give the axes some labels:
        customPlot->xAxis->setLabel( "x" );
        customPlot->yAxis->setLabel( "y" );
        // set axes ranges, so we see all data:
        customPlot->xAxis->setRange( -1, 1 );
        customPlot->yAxis->setRange( -1, 1 );
     
        customPlot ->setInteractions( QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables );
        connect( customPlot, SIGNAL(plottableClick(QCPAbstractPlottable*, QMouseEvent*)), this, SLOT(graphClicked( QCPAbstractPlottable*)));
    }

    main.cpp
    Code Qt : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #include <QApplication>
    #include "mainwindow.h"
    #include <QtWidgets/QApplication>
    #include <QtQuick>
    #include "CustomPlotItem.h"
     
    int main(int argc, char *argv[])
    {
    #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
      QApplication::setGraphicsSystem("raster");
    #endif
      QApplication a(argc, argv);
      MainWindow w;
      //w.show();
     
      qmlRegisterType<CustomPlotItem>("CustomPlot",1,0,"CustomPlotItem");
      QQuickView view(QUrl("qrc:/graph.qml"));
      view.setResizeMode(QQuickView::SizeRootObjectToView);
      view.resize(500,500);
      view.show();
     
      return a.exec();
    }

    graph.qml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import QtQuick          2.3
    import QtQuick.Controls 1.2
    import CustomPlot       1.0
     
    Item {
     
            CustomPlotItem {
     
                id: customPlot
                anchors.fill: parent
     
                Component.onCompleted: initCustomPlot()
     
            }
    }
    J'ai également en sorti le message
    QObject::connect: No such slot CustomPlotItem::realtimeDataSlot(m_customPlot) in ..\plots\customplotitem.cpp:53
    mais je ne sais pas pourquoi.

    Est-ce que vous pouvez m'aider a résoudre le pb ?
    Merci

  2. #2
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 859
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 859
    Points : 218 580
    Points
    218 580
    Billets dans le blog
    120
    Par défaut
    Bonjour,

    Essayez de faire en sorte que la fonction realtimeDataSlot() ne prenne aucun paramètre. Ainsi, vous pourrez écrire le connect de la sorte:

    Code Qt : Sélectionner tout - Visualiser dans une fenêtre à part
    connect(dataTimer, &QTimer::timeout, this, &CustomPlotItem::realtimeDataSlot);
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

Discussions similaires

  1. graphique en temps réel avec gtkdatabox
    Par neritha dans le forum GTK+
    Réponses: 1
    Dernier message: 07/06/2012, 20h54
  2. Déplacer des objets en temps réel avec la souris.
    Par johnnyjohnny dans le forum MATLAB
    Réponses: 4
    Dernier message: 03/07/2007, 14h54
  3. Bibliothèque graphique 2D temps réel
    Par gk14fire dans le forum Applications et environnements graphiques
    Réponses: 2
    Dernier message: 25/05/2007, 09h27
  4. [Temps Réel] avec du C++ ?
    Par poukill dans le forum C++
    Réponses: 35
    Dernier message: 26/02/2007, 08h22
  5. Informatique temps réel avec VxWorks
    Par Mastero dans le forum C++
    Réponses: 3
    Dernier message: 02/09/2005, 22h08

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo