PDA

View Full Version : Graph plotting



steg90
6th June 2007, 11: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.

magland
6th June 2007, 11:57
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?!


Make a layout and insert the plot window and the controls as separate components and then set the size policies appropriately.



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

Use QwtPlot from the Qwt library (http://qwt.sourceforge.net).

You may also want to have a look at ChainLink (http://chainlink.sourceforge.net).

steg90
6th June 2007, 12:07
Hi,

Thanks, the QwtPlot looks the way to go. I am using Windows, I guess I will beed to compile this library in order to use it?

Regards,
Steve

magland
6th June 2007, 12:53
Hi,

Thanks, the QwtPlot looks the way to go. I am using Windows, I guess I will beed to compile this library in order to use it?

Regards,
Steve

Three steps to using qwt:
1. Compile the library (qmake and make)
2. Link against the library (in your project file, using LIBS += ...)
3. #include the appropriate qwt headers in your code, for example "qwt_plot.h"

You will probably be interested in QwtPlot and QwtPlotCurve

steg90
6th June 2007, 12:57
Thanks,

I've tried to compile the library but not having any luck, I'm doing this for Win32. Maybe I need to convert the .pro files? I'm using MSVC.

Regards,
Steve

magland
6th June 2007, 13:46
Thanks,

I've tried to compile the library but not having any luck, I'm doing this for Win32. Maybe I need to convert the .pro files? I'm using MSVC.

Regards,
Steve

Try looking at http://docs.huihoo.com/qwt/qwtinstall.html.

steg90
6th June 2007, 14:05
Thanks, this is what I have been looking at, but still no success :confused:

Regards,
Steve

steg90
6th June 2007, 14:32
Cannot find certain header files when I'm compiling the libs, these are in my path:confused:

Header files such as sal.h, math.h...

I'd like to get this to build, it sounds ideal.

Regards,
Steve

steg90
7th June 2007, 09:52
Hi,

Having installed QWT library, I now do the following code :



QwtPlot *myPlot;
myPlot = new QwtPlot(this);


What happens when the new is called is my application just quits, anybody any ideas, I'm thinking maybe some DLL missing or something?

Regards,
Steve

steg90
7th June 2007, 11:10
I decided to build QWT again but this time with the examples, the library builds fine but I cannot build the examples, this is the error after I run nmake :

Microsoft (R) Program Maintenance Utility Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.

link /LIBPATH:"c:\Qt\4.3.0\lib" /LIBPATH:"c:\Qt\4.3.0\lib" /NOLOGO /DEBU
G /MANIFESTFILE:"obj\cpuplot.intermediate.manifest" /SUBSYSTEM:WINDOWS /OUT:debu
g\cpuplot.exe @C:\DOCUME~1\STEVEG~1.DIA\LOCALS~1\Temp\nmD2.tmp
moc_cpuplot.obj : error LNK2001: unresolved external symbol "public: static stru
ct QMetaObject const QwtPlot::staticMetaObject" (?staticMetaObject@QwtPlot@@2UQM
etaObject@@B)
debug\cpuplot.exe : fatal error LNK1120: 1 unresolved externals
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 8\VC\BIN\l
ink.EXE"' : return code '0x460'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 8\VC\BIN\n
make.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: 'cd' : return code '0x2'
Stop.
NMAKE : fatal error U1077: 'cd' : return code '0x2'
Stop.

As can be seen, it looks like it isn't linking in the qwt5.lib file, do I need to path this or something?

Any help is appreciated,
Steve

steg90
11th June 2007, 09:25
Hi,

I'm using Qwt to help me with plotting graph data. What I want is to allow the user to zoom in and out of the graph. I have been looking at the QwtPlotZoomer class and implemented the following code:



QwtPlotZoomer* zoomer = new QwtPlotZoomer(QwtPlot::xBottom,
QwtPlot::yLeft,
QwtPicker::DragSelection,
QwtPicker::AlwaysOn,
myPlot->canvas() );
zoomer->setRubberBandPen(QPen(Qt::black));
zoomer->setZoomBase();


I was expecting to be able to left click the mouse and produce a rectangle over the graph which would then zoom in, on right click it would zoom out. What do I need in order to implement this?

Any help is appreciated,
Regards,
Steve

sincnarf
5th July 2007, 20:20
Cannot find certain header files when I'm compiling the libs, these are in my path:confused:

Header files such as sal.h, math.h...

I'd like to get this to build, it sounds ideal.

Regards,
Steve

Hello Steg90 Have you installed qwt successfully and actually ran a program using it?

I'm trying to install QWT can you teach me or give me a complete guide how you made qwt work for you?

swathi
6th November 2008, 18:52
hey
when i excuted the above code
i have got the errors:crying:
can u pls tell me which header files to be included
and the start of the program :confused:

ningoji
6th April 2011, 11:30
Hi..
How can i plot graph in QT.I have to take data from a file and have to plot