Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Real time plot issues

    General and Desktop
    2
    8
    5981
    Loading More Posts
    • 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
      sirfoga last edited by

      Hi! I'm using Qt 4.8.1 with qcustomplot, and I'm trying to create a real-time plot of random data generated by a function.
      I've followed all steps of the qcustomplot example about realtime plots, I've created a main.cpp and functions.h files where
      -main.cpp contains the definition of the window and the setup of the plot
      -functions. provides 3 void functions -> setup the plot, create graph and the slot for real time data
      I've created also a variable of time Qtimer which I start after the graph is created and I've QObject::connect(ed) the pointer to the window created in main with the update of the graph...yet when I start the program the graph doesn't move (i.e data don't appear).
      I can post the source codes, if it's necessary.
      Thank you in advance!

      1 Reply Last reply Reply Quote 0
      • S
        sirfoga last edited by

        Any idea why it's not working??

        1 Reply Last reply Reply Quote 0
        • K
          koahnig last edited by

          welcome to devnet

          First of all you are in the wrong forum for detailed support on QCustomPlot. You might to check "here":http://www.qcustomplot.com/

          Second a bit more patience is typically required in this forum.

          Third also the information given is probably not sufficient to get help.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply Reply Quote 0
          • S
            sirfoga last edited by

            EDIT: source code
            main.cpp
            @#include <QtGui/QApplication>
            #include <QtGui/QLabel>
            #include <QtGui/QPushButton>
            #include <QtGui/QVBoxLayout>
            #include <QtGui/QMainWindow>
            #include <QtGui/QWidget>
            #include <QtGui/QStatusBar>

            #include "qcustomplot.h"
            #include "functions.h";

            int main(int argc, char *argv[]){
            //app containing window
            QApplication my_app(argc, argv);

            //main window customized
            QMainWindow *win = new QMainWindow;
            win->setObjectName(QString::fromUtf8("MainWindow"));
            win->resize(420,240);
            win->move(420,120);

            //making room for graph
            QWidget *centralWidget = new QWidget(win);
            centralWidget->setObjectName(QString::fromUtf8("centralWidget"));

            //create vertical layout linked to central
            QVBoxLayout *verticalLayout = new QVBoxLayout(centralWidget);
            verticalLayout->setSpacing(6);
            verticalLayout->setContentsMargins(11, 11, 11, 11);
            verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
            
            //create graph, attach to layout
            QCustomPlot *customPlot = new QCustomPlot(centralWidget);
            customPlot->setObjectName(QString::fromUtf8("customPlot"));
            verticalLayout->addWidget(customPlot);
            
            //setting up central widget
            win->setCentralWidget(centralWidget);
            
            //status bar
            QStatusBar *statusBar = new QStatusBar(win);
            statusBar->setObjectName(QString::fromUtf8("statusBar"));
            win->setStatusBar(statusBar);
            
            retranslateUi(win);
            QMetaObject::connectSlotsByName(win);
            //win->setGeometry(400, 250, 542, 390);
            
            //plotting graph
            setupPlot(customPlot, win, statusBar);
            
            win->show();
            

            //run app
            return my_app.exec();
            }@

            1 Reply Last reply Reply Quote 0
            • S
              sirfoga last edited by

              EDIT2: source code: functions.h
              @#ifndef FUNCTIONS_H
              #define FUNCTIONS_H

              //to get plot work well
              #include <QtGui/QApplication>
              #include <QtGui/QPushButton>
              #include <QtGui/QHBoxLayout>
              #include <QtGui/QMainWindow>
              #include <QtCore/QVariant>
              #include <QtGui/QAction>
              #include <QtGui/QButtonGroup>
              #include <QtGui/QHeaderView>
              #include <QtGui/QStatusBar>
              #include <QtGui/QVBoxLayout>
              #include <QtGui/QWidget>
              #include <QTimer>
              #include "qcustomplot.h"

              //pseudo random data provider
              #include <ctime>

              //DEBUG
              #include <iostream>

              //pseudo random double generator
              double fRand(double fMin, double fMax){
              //calculating double
              double f = (double)rand() / RAND_MAX;
              return fMin + f * (fMax - fMin);
              }

              //plot updater
              void realtimeDataSlot(QCustomPlot *customPlot){

              double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
              static double lastPointKey = 0;
              
              if (key - lastPointKey > 0.01){
                  //new y-values
                  //qSin(key*1.6+qCos(key*1.7)*2)*10 + qSin(key*1.2+0.56)*20 + 26;
                  //double value0 = qSin(key);
                  double value0 = fRand(-5.0, 15.0);
              
                  // plot new data -> remove old one
                  //line
                  customPlot->graph(0)->addData(key, value0);
                  customPlot->graph(0)->removeDataBefore(key-8);
                  customPlot->graph(0)->rescaleValueAxis();
                  //dot
                  customPlot->graph(1)->clearData();
                  customPlot->graph(1)->addData(key, value0);
              
                  lastPointKey = key;
              }
              
              // make key axis range scroll with the data (at a constant range size of 8):
              customPlot->xAxis->setRange(key + 0.25, 8, Qt::AlignRight);
              customPlot->replot();
              

              }

              //graph builder
              void setupRealtimeData(QCustomPlot *customPlot, QMainWindow *target){
              QTimer *dataTimer = new QTimer(target);

              //customization and feel
              customPlot->setNotAntialiasedElements(QCP::aeAll);
              QFont font;
              font.setStyleStrategy(QFont::NoAntialias);
              customPlot->xAxis->setTickLabelFont(font);
              customPlot->yAxis->setTickLabelFont(font);
              customPlot->legend->setFont(font);
              
              //pen drawing chart
              customPlot->addGraph();
              customPlot->graph(0)->setPen(QPen(Qt::blue));
              
              //dot dragging pen
              customPlot->addGraph();
              customPlot->graph(1)->setPen(QPen(Qt::blue));
              customPlot->graph(1)->setLineStyle(QCPGraph::lsNone);
              customPlot->graph(1)->setScatterStyle(QCPScatterStyle::ssDisc);
              
              //filling plot with custom colour hex: ##F59725 -> RGB: (245, 151, 37) -> QColor(245, 151, 37)
              customPlot->graph(0)->setBrush(QBrush(QColor(245, 151, 37)));
              customPlot->graph(0)->setAntialiasedFill(false);
              
              //printing time in x-axis every 2 seconds
              customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
              customPlot->xAxis->setDateTimeFormat("hh:mm:ss");
              customPlot->xAxis->setAutoTickStep(false);
              customPlot->xAxis->setTickStep(2);
              customPlot->axisRect()->setupFullAxesBox();
              
              // make left and bottom axes transfer their ranges to right and top axes:
              QObject::connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
              QObject::connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
              
              // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
              QObject::connect(dataTimer, SIGNAL(timeout()), target, SLOT(realtimeDataSlot(customPlot)));
              
              //ms that elapse between 2 consecutive updates of graph (0 = as fast as possible)
              dataTimer->start(200);
              

              }

              //real time data plotter
              void setupPlot(QCustomPlot *customPlot, QMainWindow *target, QStatusBar *statusBar){
              //plotting
              setupRealtimeData(customPlot, target);

              //window title
              target->setWindowTitle("Here goes title");
              statusBar->clearMessage();
              
              //updating
              customPlot->replot();
              

              }

              //window mover
              void retranslateUi(QMainWindow *MainWindow){
              MainWindow->setWindowTitle(QApplication::translate("MainWindow", "Plot examples", 0, QApplication::UnicodeUTF8));
              }

              #endif@

              1 Reply Last reply Reply Quote 0
              • K
                koahnig last edited by

                You might want to check out how to use "signals and slots.":http://doc.qt.io/qt-5/signalsandslots.html

                I gues the connect statements give you already a warning on output screen. This tells you that the connects are not possible.

                realtimeDataSlot is not a member function of QMainWindow.

                You need to derive at least a class from QObject in order to get this to work.

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply Reply Quote 0
                • S
                  sirfoga last edited by

                  koahnig thank you, in fact when runnning application shell outouts: "Object::connect: No such slot QMainWindow::realtimeDataSlot(customPlot)", so, what should I do?

                  1 Reply Last reply Reply Quote 0
                  • K
                    koahnig last edited by

                    In my previous post I have given you already a link to the documentation of signals and slots.

                    When using Qt you need to have a sound understanding of C++ and OOP. If you are familiar with the terminology and methods of OOP you can start looking into Qt tutorials as well as the documentation of signals and slots.

                    When you like to use QCustomPlot you probably have to go through their tutorials as well. I am not familiar with this at all.

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post