Hi,

I've come up with the following simple code to draw a x,y graph plot ( the graph is updated in real-time ) :

Qt Code:
  1. void PlotterView::paintEvent( QPaintEvent *event)
  2. {
  3. QPainter painter(this);
  4.  
  5. painter.setWindow( 0, 0, 800, 200 );
  6.  
  7. painter.setRenderHint( QPainter::Antialiasing, true);
  8. painter.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
  9.  
  10. int nGridStep = 20;
  11. for( int x = 0; x <= 800 / nGridStep; x ++ )
  12. {
  13. painter.drawLine( x * nGridStep, 0, x * nGridStep, 200 );
  14. }
  15. for( int y = 0; y <= 200 / nGridStep; y ++ )
  16. {
  17. painter.drawLine( 0, y * nGridStep, 800, y * nGridStep );
  18. }
  19.  
  20. painter.setPen(QPen(Qt::red, 3, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
  21. int nSpace = 0;
  22. int nSpace2 = 0;
  23. for( int i = 0; i < 48; i ++ ) // size of linear buffer
  24. {
  25. nSpace += nGridStep;
  26. painter.drawLine( nSpace2, 200 - m_GraphArray[ i ], nSpace, 200 - m_GraphArray[ i + 1 ] );
  27. nSpace2 += nGridStep;
  28. }
  29. }
To copy to clipboard, switch view to plain text mode 

m_GraphArray is an array of unsigned ints which gets updated as a 'rolling buffer', at the moment, the graph is plotting randomly within a timer :

Qt Code:
  1. void PlotterView::timerEvent(QTimerEvent *event)
  2. {
  3. memcpy( m_GraphArray, &m_GraphArray[1], 50 * sizeof(unsigned int) ); // move entire array left one
  4. m_GraphArray[50] = qrand() % 200;
  5. repaint();
  6. }
To copy to clipboard, switch view to plain text mode 

What I like about this, is the fact that if the user stretches the window this widget is in, the graph also stretches etc...

Ok, now my question, finally... If I wanted to have other controls within the window the graph is in, how do I do it so when the user stretches the window, the graph doesn't go over these controls? Does this make sense?!

I was thinking I may have a subclassed label or something and use that to draw the graph on and just drop this onto a new window?

Regards,
Steve

PS - If there is also a better way of drawing graphs instead of using the QPainter class I'd be very interested.