Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainter read data and plot real time graph.
Forum Update on Monday, May 27th 2025

QPainter read data and plot real time graph.

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.9k 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.
  • N Offline
    N Offline
    Nopa11
    wrote on last edited by
    #1

    Hello everybody, I'm a beginner. I'm using qt in my project and try to let qt working read ADC and plot graph real time from paint event. Now I can read ADC and show on lcd widget realtime, but when I add a painter code to plot a graph. I can build it but when I run a program the window is suddenly closed. I don't know where is the critrically error that make a program suddenly closed. Thanks

    Here it's my code

    @#ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog(QWidget *parent = 0);
    int a2dVal;
    int *a2dplot;
    int i;

    ~Dialog();
    

    private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void read_ADC();
    void paintEvent(QPaintEvent *e);

    private:
    Ui::Dialog *ui;
    };

    #endif // DIALOG_H
    @

    @
    #include "dialog.h"
    #include "ui_dialog.h"
    #include "mcp3008Spi.h"
    #include <QTimer>
    #include <QtCore>
    #include <QtGui>

    QTimer*timer;

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(read_ADC()));
    }
    Dialog::~Dialog()
    {
    delete ui;
    }
    void Dialog::read_ADC()
    {
    mcp3008Spi a2d("/dev/spidev0.0", SPI_MODE_0, 1000000, 8);

    int a2dChannel = 0;
    unsigned char data[3];
    
        data[0] = 1; 
        data[1] = 0b10000000 |( ((a2dChannel & 7) << 4)); 
        data[2] = 0; 
    
        a2d.spiWriteRead(data, sizeof(data) );
        a2dVal = 0;
                a2dVal = (data[1]<< 8) & 0b1100000000; 
                a2dVal |=  (data[2] & 0xff);
    
        a2dplot[i] = a2dVal;
    
        ui->lcdNumber->display(a2dVal);
        i++;
    

    }
    void Dialog::on_pushButton_clicked()
    {
    timer ->start(10);
    i = 0;
    }
    void Dialog::on_pushButton_2_clicked()
    {
    timer -> stop();
    }
    void Dialog::paintEvent(QPaintEvent e)
    {
    QPainter painter(this);
    if(i>0)
    {
    painter.setPen( QPen(Qt::red, 5,Qt::SolidLine) );
    painter.drawLine( i
    100, a2dplot[i-1], (i+1)*100, a2dplot[i]);
    }
    }
    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SysTech
      wrote on last edited by
      #2

      Unless I missed it you declare:
      @
      int *a2dplot;
      @
      as a pointer to an int but nowhere to you make it point to anything? However in your Read_ADC you have:

      @ a2dplot[i] = a2dVal;@

      So you are writing to who knows where. I could have missed your assignment.

      But one suggestion is instead of using int *a2dplot, use a container like a qvector. This way you can add data without having to worry about it and you can easily traverse the data using the iterators.

      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