Qt World Summit: Submit your Presentation

QDateTime as float for QChart?

  • I dont find an easy conversion of a QDateTime to a float (e.g. qfloat). What I have in mind is the spreadsheet-like format, where integer part is days since X (I believe MSExcel uses jan 1 1900, other flavors may be different) and float part is fraction of the day. I find that format very convenient. My goal is to use QChart with the axes being either QValueAxis or QDateTimeAxis, particularly for the horizontal axis (asisX()). In the current implementation, the min() and setMin() methods use QDateTime for QDateTimeAxis and qreal for QValueAxis. I would like an overloaded set of methods so I could use qreal values for both. Any thoughts??

  • You can use "secTo":http://qt-project.org/doc/qt-5/qdatetime.html#secsTo to compute seconds as integer and divide by 86400.0 to obtain a float/double. Checkout if you can use Jan 1, 1900.

  • Thanks for response, I have been using that. I am developing a plotting widget that is general enough so that in code, I will not necessarily know if the axisX is QValue or QDateTime. So, right now I have to do the following to get max and min values from axisX():

    QDateTimeAxis dtax=dynamic_cast<QDateTimeAxis*>(axisX());
    QValueAxis valax=dynamic_cast<QValueAxis>(axisX());

    Then check for which one came back NULL. If DateTime, then do the conversion as you suggest. If Value, then simply make a call to min() and max().

    Because I cant get qreal min() from a QDateTimeAxis, it is more lines of code.

Log in to reply