Results 1 to 14 of 14

Thread: Graph plotting

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Graph plotting

    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.

  2. #2
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Graph plotting

    Quote Originally Posted by steg90 View Post
    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?!
    Make a layout and insert the plot window and the controls as separate components and then set the size policies appropriately.

    Quote Originally Posted by steg90 View Post
    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.

    You may also want to have a look at ChainLink.

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Graph plotting

    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

  4. #4
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Graph plotting

    Quote Originally Posted by steg90 View Post
    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

  5. The following user says thank you to magland for this useful post:

    sincnarf (16th October 2007)

  6. #5
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Graph plotting

    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

  7. #6
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Graph plotting

    Quote Originally Posted by steg90 View Post
    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.

  8. #7
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Graph plotting

    Thanks, this is what I have been looking at, but still no success

    Regards,
    Steve

  9. #8
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Graph plotting

    Cannot find certain header files when I'm compiling the libs, these are in my path

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

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

    Regards,
    Steve

  10. #9
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Graph plotting

    Hi,

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

    Qt Code:
    1. QwtPlot *myPlot;
    2. myPlot = new QwtPlot(this);
    To copy to clipboard, switch view to plain text mode 

    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

  11. #10
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Graph plotting

    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

  12. #11
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Graph plotting

    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:

    Qt Code:
    1. QwtPlotZoomer* zoomer = new QwtPlotZoomer(QwtPlot::xBottom,
    2. QwtPlot::yLeft,
    3. QwtPicker::DragSelection,
    4. QwtPicker::AlwaysOn,
    5. myPlot->canvas() );
    6. zoomer->setRubberBandPen(QPen(Qt::black));
    7. zoomer->setZoomBase();
    To copy to clipboard, switch view to plain text mode 

    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

  13. #12
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Graph plotting

    Quote Originally Posted by steg90 View Post
    Cannot find certain header files when I'm compiling the libs, these are in my path

    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?

  14. #13
    Join Date
    Nov 2008
    Posts
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Question Re: Graph plotting

    hey
    when i excuted the above code
    i have got the errors
    can u pls tell me which header files to be included
    and the start of the program

  15. #14
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Graph plotting

    Hi..
    How can i plot graph in QT.I have to take data from a file and have to plot

Similar Threads

  1. Qt Graph Editor
    By Stiefel in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2018, 08:44
  2. Threading and plotting graph in same program.
    By sar_van81 in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 9th May 2007, 21:42
  3. regarding graph plotting in qt
    By sar_van81 in forum Qt for Embedded and Mobile
    Replies: 11
    Last Post: 30th April 2007, 09:51
  4. Plotting plug-in available?
    By brcain in forum Qt Programming
    Replies: 2
    Last Post: 12th September 2006, 23:58
  5. How to plot a graph in Qt ?
    By vinod in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2006, 14:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.