Results 1 to 2 of 2

Thread: Implementation differences between QRect and QRectF

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2012
    Posts
    66
    Platforms
    Windows
    Thanks
    20
    Thanked 2 Times in 2 Posts

    Default Implementation differences between QRect and QRectF

    I was looking at the Qt source (qrect.h), and I noticed a difference between how QRect and QRectF are implemented: QRect's private members are four numbers representing the top left and bottom right point, while QRectF's private members are four numbers representing a point and a width and height. Why are they implemented differently?

    The reason I'm asking is that, for example, if you want to access the width of a rectangle inside a loop, you might do this:

    Qt Code:
    1. for (int i = 0; i < 100000; ++i)
    2. {
    3. doSomeCalculation(someQRectF.width());
    4. }
    To copy to clipboard, switch view to plain text mode 

    This would make sense if using a QRectF, because QRectF::width() just returns the private member variable QRectF::w. Thus, the width isn't being calculated every iteration. However, it would not make sense for QRect, since QRectF::width() calculates the width by subtracting x1 from x2. So ideally the same function using a QRect should look like this:

    Qt Code:
    1. int rectWidth = someQRect.Width(); // only calculate the width once
    2. for (int i = 0; i < 100000; ++i)
    3. {
    4. doSomeOtherCalculation(rectWidth);
    5. }
    To copy to clipboard, switch view to plain text mode 

    I realize this is micro-optimization talk, but, nonetheless, what's the reason for this?
    Last edited by wayfaerer; 30th December 2012 at 10:48.

Similar Threads

  1. Resize QRectF in drawText when necessary
    By estanisgeyer in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2010, 14:27
  2. difference of QRect and QRectF
    By gbmtoday in forum Newbie
    Replies: 1
    Last Post: 14th January 2010, 22:52
  3. Rotate a QRectF
    By whitefurrows in forum Qt Programming
    Replies: 5
    Last Post: 15th July 2009, 15:33
  4. Error in QRectF::intersects
    By lni in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2009, 17:24
  5. QRectF and drawText
    By aronsson in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2006, 16:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.