Q:

QT painter update with timer only update when close window

I have some data will change continuously.  I draw these data into line via QPainter and want to do dynamic update(update every second) via QTimer but data will only update when I close the painting window, it will not real time update in window.  Where am I wrong?? 
Here is the code:
#include <QtGui/QApplication>
#include <QApplication>
#include <QLabel>
#include <QWidget>
#include <QPainter>
#include <QTimer>

#define WINDOW_H 512
#define WINDOW_W 512

unsigned char *pMergedData; 

class DrawDemo : public QWidget {
    Q_OBJECT
public: 
    DrawDemo( QWidget *parent=0); 
public slots:
    void MyUpdate();
protected:
    void paintEvent(QPaintEvent*);
private:
    QTimer *timer; 
}; 

void DrawDemo::MyUpdate(){
    test_plot();
    update();
}

DrawDemo::DrawDemo( QWidget *parent) :QWidget(parent){ 
    pMergedData = (unsigned char *)malloc(200*sizeof(short));
    QTimer *timer = new QTimer(this); 
    connect( timer, SIGNAL( timeout() ), this, SLOT(  MyUpdate()  ) ); 
    timer->start( 1000 ); //ms 
} 

void DrawDemo::paintEvent( QPaintEvent * ) {
    short *buf16 = (short *)pMergedData;

    QPainter painter( this );
    QPoint beginPoint;
    QPoint endPoint; 

    painter.setPen(QPen(Qt::red, 1));
    for( int i=0; i<199; i++ ) {
        beginPoint.setX( 2*i );
        beginPoint.setY( WINDOW_H - buf16[i] ); 
        endPoint.setX( 2*i+1 );
        endPoint.setY( WINDOW_H - buf16[i+1]);
        painter.drawLine( beginPoint, endPoint );
    }
} 

int test_plot(){

    counter_globol ++;
    if(counter_globol%2==0){
        for(int i=0; i<200; i++ ) { 
            pMergedData[i] = 100;
         } 
    }else{
            for(int i=0; i<200; i++ ) { 
            pMergedData[i] = i;
        } 
    }

    return 0;

}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    DrawDemo *drawdemo = new DrawDemo( 0 ); 
    drawdemo->setWindowTitle("QPainter");
    drawdemo->resize(WINDOW_W, WINDOW_H);
    drawdemo->show();

    a.exec();
    free(pMergedData);
    return 0;
}

A:

The update only occurs when you close the window because the structure of your code is wrong. 
The a.exec() function starts the main event loop for the program, which handles all the events that are occurring, such as mouse movements, button presses and timers.  It doesn't exit the function until the window is closed.  At this time, a.exec() expects the program is finishing. 
In the code you've posted, it sets up the data for lines, creates the widget and then starts the message handler with a.exec().  Now, the lines are never going to change because the data in pMergedData is never called again, until the window is closed, but a.exec() should not be handled this way. 
When you close the window and reopen it, the a.exec() function returns and due to your while loop, the data in pMergedData is reinitialised and a new window is created. 
So, to fix this you need something like this: -
int main(int argc, char *argv[])
{
    QApplication a;

    DrawDemo *drawdemo = new DrawDemo( 0 ); 
    drawdemo->setWindowTitle("QPainter");
    drawdemo->resize(WINDOW_W, WINDOW_H);
    drawdemo->show();   

    a.exec();
    return 0;
}

As you see, I've moved the creation of QApplication and the DrawDemo object out of the test_plot function.  As you want the data to change each second, you should also be calling test_plot each second, so instead of having the timer call the widget function update every second, create your own function MyUpdate and connect to that instead: -
void DrawDemo::MyUpdate()
{
    test_plot();
    update();
}

