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:
Code:
{
public:
baseTime(initialTime)
{
}
virtual QwtText label
(double v
) const {
QDateTime upTime
= baseTime.
addMSecs((int)v
);
return upTime.time().toString();
}
void updateBaseTime
(const QDateTime &updatedInitialTime
) {
baseTime = updatedInitialTime;
}
private:
};
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:
Code:
{
// mBaseTime is a QDateTime member of the class
double msecsToLatest = mBaseTime.msecsTo(latestDisplayed);
if (msecsToLatest > displayedRangeInMsecs)
{
mBaseTime = latestDisplayed.addMSecs(-displayedRangeInMsecs);
TimeScaleDraw
* scaleDraw
= (TimeScaleDraw
*)axisScaleDraw
(QwtPlot::xBottom);
scaleDraw->updateBaseTime(mBaseTime);
setAxisScale
(QwtPlot::xBottom,
0, displayedRangeInMsecs
);
}
return mBaseTime;
}
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.
Code:
// These two lines replaced
//TimeScaleDraw* scaleDraw = (TimeScaleDraw*)axisScaleDraw(QwtPlot::xBottom);
//scaleDraw->updateBaseTime(mBaseTime);
// with this:
setAxisScaleDraw
(QwtPlot::xBottom,
new TimeScaleDraw
(mBaseTime
));
I'd still love to hear why my previous solution wouldn't work.
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.
Code:
void updateBaseTime
(const QDateTime &updatedInitialTime
) {
baseTime = updatedInitialTime;
invalidateCache();
}
Uwe
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.