QPainter read data and plot real time graph.

  • 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

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


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

    Ui::Dialog *ui;

    #endif // DIALOG_H

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


    Dialog::Dialog(QWidget *parent) :
    ui(new Ui::Dialog)
    timer = new QTimer(this);
    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;

    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);
    painter.setPen( QPen(Qt::red, 5,Qt::SolidLine) );
    painter.drawLine( i
    100, a2dplot[i-1], (i+1)*100, a2dplot[i]);

  • 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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.