Results 1 to 13 of 13

Thread: Using QwtPlot to plot binary data from file

  1. #1
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Using QwtPlot to plot binary data from file

    Hi

    I have some data files which I would like to plot using Qwt. The setData member function requires data of type double, however my data could be uchar, int or float. I know I could copy the data to an array of type double and then pass this to setData. Is there a better way of doing this?

    Regards

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QwtPlot to plot binary data from file

    Derive YourData from QwtData, and implement a bridge to your array. Be careful with reimplementing YourData::copy: you don't need to copy the values - only the interface.

    Note, that QwtPlotCurve always iterates over the samples in increasing order. So you don't need to have all your data in memory at the same time.

    Uwe

  3. #3
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QwtPlot to plot binary data from file

    Thanks Uwe

    You have lost me, could you elaborate on
    "implementing a bridge to my array "
    and
    "with reimplementing YourData::copy: you don't need to copy the values - only the interface"

    Regards

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QwtPlot to plot binary data from file

    Qt Code:
    1. class YourData: public QwtData
    2. {
    3. public:
    4. YourData(uchar *values, int size):
    5. m_values(values),
    6. m_size(size)
    7. {
    8. }
    9.  
    10. virtual size_t size() const
    11. {
    12. return m_size;
    13. }
    14.  
    15. virtual double x(size_t i) const
    16. {
    17. return (double)m_values[2 * i];
    18. }
    19.  
    20. virtual double y(size_t i) const
    21. {
    22. return (double)m_values[2 *i + 1];
    23. }
    24.  
    25. virtual QwtData *copy() const
    26. {
    27. return new YourData(m_values, m_size);
    28. }
    29. };
    To copy to clipboard, switch view to plain text mode 

    The default implementation of QwtData::boundingRect iterates over all values. If you have many points and you know your bounding rect I recommend to implement YoutData::boundingRect too.

    If possible I would recommend to use a QVector instead. Because it implicitely shares the values you can copy it and it is more safe, when you want to unload your values.

    And yes passing a pointer of QwtData would have been a better API than this copy construction.

    Uwe

  5. The following 2 users say thank you to Uwe for this useful post:

    dbrmik (30th January 2009), develofer (27th November 2009)

  6. #5
    Join Date
    Oct 2008
    Posts
    74
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QwtPlot to plot binary data from file

    Thanks Uwe

    You have been a great help. Just one question about boundingRect, What is this used for?

    Regards

  7. #6
    Join Date
    Mar 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Using QwtPlot to plot binary data from file

    hi
    I am charu, totally New to Qwt , i have to plot from .txt file containing floating point. i want to know how to load/read a datafile and plot it row by row (that is: one value by one)
    thank you for ur grt help

  8. #7
    Join Date
    Sep 2009
    Posts
    57
    Thanks
    7
    Thanked 5 Times in 4 Posts

    Default Re: Using QwtPlot to plot binary data from file

    Hi,

    I have implemented a realtime plot using my own Qwtdata interface. Each time I plot the curve a new copy of the interface is created. This is a small memory leak which leads to a big one over an extended period of time.

    I cannot use an array without having to do a lot of memory shifting which is too expensive given I replot 5 times a second for a period ranging between a few minutes to many hours.

    Is there a better way to do this and avoid the memory leak?

    Here is a snippet;

    Qt Code:
    1. double spdData[50];
    2. int spdCur;
    3.  
    4. // Speed history
    5. double RealtimeSpdData::x(size_t i) const { return (double)50-i; }
    6. double RealtimeSpdData::y(size_t i) const { return spdData[(spdCur+i) < 50 ? (spdCur+i) : (spdCur+i-50)]; }
    7. size_t RealtimeSpdData::size() const { return 50; }
    8. QwtData *RealtimeSpdData::copy() const { return new RealtimeSpdData(); } //<<<LEAK
    9. void RealtimeSpdData::init() { spdCur=0; for (int i=0; i<50; i++) spdData[i]=0; }
    10. void RealtimeSpdData::addData(double v) { spdData[spdCur++] = v; if (spdCur==50) spdCur=0; }
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QwtPlot to plot binary data from file

    Qt Code:
    1. QwtData *RealtimeSpdData::copy() const { return new RealtimeSpdData(); } //<<<LEAK
    To copy to clipboard, switch view to plain text mode 
    QwtPlotCurve::setData() always deletes the previously allocated data object before it creates a new clone ( check the code ). So I guess you are calling RealtimeSpdData::copy in your code without releasing the clone or the leak is from somewhere else. ( What about the object you pass to QwtPlotCurve::setData() - this one needs to be relased by the application ).

    By the way: you don't need to create a new QwtData object when ever your data changes - it's enough to call plot->replot(). If you need the auto-replot feature you also have to call curve->itemChanged() after each addData().

    Maybe its worth to implement RealtimeSpdData::boundingRect(). The default implementation iterates over all points, what could be done ore efficient in your case. For 50 points this will be an unimportant optimization, but maybe you want to have more points later.

    Uwe

  10. #9
    Join Date
    Sep 2009
    Posts
    57
    Thanks
    7
    Thanked 5 Times in 4 Posts

    Default Re: Using QwtPlot to plot binary data from file

    Quote Originally Posted by Uwe View Post
    QwtPlotCurve::setData() always deletes the previously allocated data object before it creates a new clone ( check the code ). So I guess you are calling RealtimeSpdData::copy in your code without releasing the clone or the leak is from somewhere else. ( What about the object you pass to QwtPlotCurve::setData() - this one needs to be relased by the application ).

    By the way: you don't need to create a new QwtData object when ever your data changes - it's enough to call plot->replot(). If you need the auto-replot feature you also have to call curve->itemChanged() after each addData().

    Maybe its worth to implement RealtimeSpdData::boundingRect(). The default implementation iterates over all points, what could be done ore efficient in your case. For 50 points this will be an unimportant optimization, but maybe you want to have more points later.

    Uwe
    Thanks for the quick response. That's excellent. I guess my leak is somewhere else :-) I thought copy() was called by each replot but obviously not (I should have checked more thoroughly before posting, my apologies).

    FWIW, the scrolling graph works really nicely (flicker free, smooth) on Win32, Mac and Linux.

    Good Job Uwe!

  11. #10
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Using QwtPlot to plot binary data from file

    I have a small question that could be obvious for many of you, but not for me...

    Once you have your data inherited from QwtData
    (e.g. in my case I have float numbers)

    How do you pass it to setData (or setRawData in my case)?

    Here the definitions of setData and setRawData:
    (From qwtplotcurve manual)

    Qt Code:
    1. void QwtPlotCurve::setData ( const double * xData, const double * yData, int size )
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void QwtPlotCurve::setRawData ( const double * xData, const double * yData, int size )
    To copy to clipboard, switch view to plain text mode 

    I do not see the link in between my YourData (derived from QwtData) and how to "link" it to my QwtPlot...

    Thank you...

  12. #11
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QwtPlot to plot binary data from file

    Qt Code:
    1. void QwtPlotCurve::setData(const QwtData &data);
    To copy to clipboard, switch view to plain text mode 

    All other setData methods are convenience methods creating an internal QwtData object. Passing values to your individual data object needs to be done via the API of your derived data class.

    Uwe

  13. #12
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QwtPlot to plot binary data from file

    Hi, I also want to plot some data from a data file. I have QWT 6.0 and it doesn't include QwtData. Does anyone know where I can get this code, or if there is another way to make a plot from a data file with the tools in qwt 6.0?

    Thanks

  14. #13
    Join Date
    Sep 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QwtPlot to plot binary data from file

    Hi,
    I just posted a thread about using data from a file to make a plot so I guess it doesn't hurt to ask here too.
    Basically after reading the whole thread and trying to make sense of things, I still don't full understand... >.<

    My main problem is:
    The data is in a file; I want to make a bridge to this data...
    Does this mean a bridge to the data file? or has the data already been read in somewhere. If so, then how?
    somewhere mentioned above is a "bridge to an array", but my data is too big to fit into an array and multi-dimensional arrays dont seem to work (for qwtplot).

    Any help will be appretiated!
    Thanks

Similar Threads

  1. Replies: 15
    Last Post: 27th May 2008, 01:46
  2. Reading binary data
    By vermarajeev in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 09:14
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10
  5. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17

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.