Results 1 to 3 of 3

Thread: QwtPlot QDateTime / QTime as X-axis - changing base time

  1. #1
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QwtPlot QDateTime / QTime as X-axis - changing base time

    I saw how the CPU plot sample utilized subclassing QwtScaleDraw to make sure the displayed labels texts show the time. I've modified the class to use QDateTime instead of QTime and allowed to change the base time:

    Qt Code:
    1. class TimeScaleDraw: public QwtScaleDraw
    2. {
    3. public:
    4. TimeScaleDraw(const QDateTime &initialTime):
    5. baseTime(initialTime)
    6. {
    7. }
    8. virtual QwtText label(double v) const
    9. {
    10. QDateTime upTime = baseTime.addMSecs((int)v);
    11. return upTime.time().toString();
    12. }
    13. void updateBaseTime(const QDateTime &updatedInitialTime)
    14. {
    15. baseTime = updatedInitialTime;
    16. }
    17. private:
    18. QDateTime baseTime;
    19. };
    To copy to clipboard, switch view to plain text mode 

    In my scenario I want to make sure the graph only displays the latest 10 seconds of data. On some updates I detect if the base time should be modified:

    Qt Code:
    1. QDateTime MyPlot::setXAxisScale(QDateTime latestDisplayed, double displayedRangeInMsecs)
    2. {
    3. // mBaseTime is a QDateTime member of the class
    4. double msecsToLatest = mBaseTime.msecsTo(latestDisplayed);
    5.  
    6. if (msecsToLatest > displayedRangeInMsecs)
    7. {
    8. mBaseTime = latestDisplayed.addMSecs(-displayedRangeInMsecs);
    9.  
    10. TimeScaleDraw* scaleDraw = (TimeScaleDraw*)axisScaleDraw(QwtPlot::xBottom);
    11.  
    12. scaleDraw->updateBaseTime(mBaseTime);
    13.  
    14. setAxisScale(QwtPlot::xBottom, 0, displayedRangeInMsecs);
    15. }
    16.  
    17. return mBaseTime;
    18. }
    To copy to clipboard, switch view to plain text mode 

    Immediately after this code I draw points and call replot();

    Even though the base time is updated the scale doesn't change. The expected behavior would be that my 0 becomes the updated base time.

    Is there anything I'm missing? Is there a method to call for redrawing of the scale map?


    Added after 19 minutes:


    For now I have resolved this issue by always initializing a new TimeScaleDraw instead of updating the baseTime member.

    Qt Code:
    1. // These two lines replaced
    2. //TimeScaleDraw* scaleDraw = (TimeScaleDraw*)axisScaleDraw(QwtPlot::xBottom);
    3. //scaleDraw->updateBaseTime(mBaseTime);
    4.  
    5. // with this:
    6. setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw(mBaseTime));
    To copy to clipboard, switch view to plain text mode 

    I'd still love to hear why my previous solution wouldn't work.
    Last edited by frankiefrank; 6th February 2011 at 18:42.

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

    Default Re: QwtPlot QDateTime / QTime as X-axis - changing base time

    QwtScaleDraw uses an internal cache, that needs to be invalidated when you change your base time.

    Qt Code:
    1. void updateBaseTime(const QDateTime &updatedInitialTime)
    2. {
    3. baseTime = updatedInitialTime;
    4. invalidateCache();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Uwe

  3. The following user says thank you to Uwe for this useful post:

    frankiefrank (8th February 2011)

  4. #3
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QwtPlot QDateTime / QTime as X-axis - changing base time

    Thanks for the tip. I see that this causes the scale map to refresh but it doesn't happen immediately. I call the update every 500 msecs and I only see the scale update once every 2-10 secs (it's not constant).

    For now it seems like initializing a new scale map each time with the wanted base time achieves the behavior I wanted.

Similar Threads

  1. Changing Slider Base
    By eLancaster in forum Newbie
    Replies: 1
    Last Post: 5th December 2010, 16:28
  2. QwtPlot axis decmials/notation
    By dfirestone in forum Qwt
    Replies: 1
    Last Post: 1st May 2009, 10:42
  3. How to convert unix time to QDateTime
    By lni in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2007, 00:25
  4. QDateTime GMT add sec. or - sec. from locale time....
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2007, 16:39
  5. Extending QDate, QTime and QDateTime
    By jamadagni in forum Qt Programming
    Replies: 8
    Last Post: 24th March 2006, 15:51

Tags for this Thread

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.