QVector2D, QSizeF and QPointF : they all hold 2 floating point values
-
wrote on 18 Aug 2010, 20:04 last edited by
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?
-
wrote on 22 Aug 2010, 08:31 last edited by
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;@
-
wrote on 22 Aug 2010, 21:51 last edited by
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.
-
wrote on 23 Aug 2010, 12:19 last edited by
I did say it wouldn't be really nice.
-
wrote on 20 Jun 2011, 13:03 last edited by
would be nice to do QPointF + QSizeF = QPointF
-
wrote on 20 Jun 2011, 13:09 last edited by
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.