Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. QVector2D, QSizeF and QPointF : they all hold 2 floating point values
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Brainstorm
6 Posts 4 Posters 5.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hugo
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      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;@

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hugo
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

          I did say it wouldn't be really nice.

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • T Offline
            T Offline
            thebootroo
            wrote on last edited by
            #5

            would be nice to do QPointF + QSizeF = QPointF

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved