Results 1 to 2 of 2

Thread: Implementation differences between QRect and QRectF

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

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Implementation differences between QRect and QRectF

    Quote Originally Posted by wayfaerer View Post
    I realize this is micro-optimization talk, but, nonetheless, what's the reason for this?
    Most probably "historical reasons":

    Quote Originally Posted by QRect docs
    The rationale is that for historical reasons the values returned by the bottom() and right() functions deviate from the true bottom-right corner of the rectangle
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    wayfaerer (30th December 2012)

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.