steg90
6th June 2007, 10:02
Hi,
I've come up with the following simple code to draw a x,y graph plot ( the graph is updated in real-time ) :
void PlotterView::paintEvent( QPaintEvent *event)
{
QPainter painter(this);
painter.setWindow( 0, 0, 800, 200 );
painter.setRenderHint( QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
int nGridStep = 20;
for( int x = 0; x <= 800 / nGridStep; x ++ )
{
painter.drawLine( x * nGridStep, 0, x * nGridStep, 200 );
}
for( int y = 0; y <= 200 / nGridStep; y ++ )
{
painter.drawLine( 0, y * nGridStep, 800, y * nGridStep );
}
painter.setPen(QPen(Qt::red, 3, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
int nSpace = 0;
int nSpace2 = 0;
for( int i = 0; i < 48; i ++ ) // size of linear buffer
{
nSpace += nGridStep;
painter.drawLine( nSpace2, 200 - m_GraphArray[ i ], nSpace, 200 - m_GraphArray[ i + 1 ] );
nSpace2 += nGridStep;
}
}
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 :
void PlotterView::timerEvent(QTimerEvent *event)
{
memcpy( m_GraphArray, &m_GraphArray[1], 50 * sizeof(unsigned int) ); // move entire array left one
m_GraphArray[50] = qrand() % 200;
repaint();
}
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...:p 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.
I've come up with the following simple code to draw a x,y graph plot ( the graph is updated in real-time ) :
void PlotterView::paintEvent( QPaintEvent *event)
{
QPainter painter(this);
painter.setWindow( 0, 0, 800, 200 );
painter.setRenderHint( QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
int nGridStep = 20;
for( int x = 0; x <= 800 / nGridStep; x ++ )
{
painter.drawLine( x * nGridStep, 0, x * nGridStep, 200 );
}
for( int y = 0; y <= 200 / nGridStep; y ++ )
{
painter.drawLine( 0, y * nGridStep, 800, y * nGridStep );
}
painter.setPen(QPen(Qt::red, 3, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin));
int nSpace = 0;
int nSpace2 = 0;
for( int i = 0; i < 48; i ++ ) // size of linear buffer
{
nSpace += nGridStep;
painter.drawLine( nSpace2, 200 - m_GraphArray[ i ], nSpace, 200 - m_GraphArray[ i + 1 ] );
nSpace2 += nGridStep;
}
}
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 :
void PlotterView::timerEvent(QTimerEvent *event)
{
memcpy( m_GraphArray, &m_GraphArray[1], 50 * sizeof(unsigned int) ); // move entire array left one
m_GraphArray[50] = qrand() % 200;
repaint();
}
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...:p 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.