QVector2D, QSizeF and QPointF : they all hold 2 floating point values

  • In this topic I would like to question some Qt design decisions:

    1- QVector2D holds 2 private variables, x and y. If instead it would hold an array with size two ( m_data[ 2 ] ) then we could implement the operator[] and access x and y using indexes (integers), which is more convenient than x() and y() in many situations.

    2- Because QVector2D, QSizeF and QPointF all store 2 floating point values, why not have QPointF and QSizeF derived from QVector2D so that we could convert between types using the operator=.

    3- If QVector2D was templated it could be used as base class both for QSize, QSizeF, QPoint and QPointF.

    Does any of these make sense?

  • You can still implement the [] operator. Probably you won't have a 'nice' implementation, because it would be something like

    @ Q_ASSERT(idx == 0 || idx == 1, "Index out of bounds");

    if (idx == 0) return x;

    if (idx == 1) return y;@

  • The purpose of using an array with size two ( m_data[ 2 ] ) is exactly to avoid the 'if's. One of the reasons I like Qt is because it is well written. Having if statements in this case just seems wrong to me.

  • I did say it wouldn't be really nice.

  • would be nice to do QPointF + QSizeF = QPointF

  • I think maintainability is more important than you avoiding an if statement. It is hard to understand what piece of data is where if you use an array to store them. That may lead to bugs in Qt in the future. So please, don't.

    Adding a convenience function that implements operator is fine by me, but the implementation would basically look like FrankZ's proposal.

    I am not sure if adding a size to a point should yield another point, but perhaps it should. That would be easy to implement. You might want to supply a merge request containing the patch and a use case and/or other argumentation why this is needed.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.