Results 1 to 4 of 4

Thread: Misscalculations when calculating percentages inside Rectangle

  1. #1
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Misscalculations when calculating percentages inside Rectangle

    Captură de ecran din 2016-12-16 20-47-37.png

    Qt Code:
    1. Rectangle {
    2. id: delegateRectangle
    3. height: list.height
    4. width: list.height * 50 / 100
    5. color: "purple"
    6. Rectangle {
    7. id: member1
    8. height: delegateRectangle.height * 20 / 100
    9. width: delegateRectangle.width
    10. anchors.top: parent.top
    11. anchors.left: parent.left
    12. color: "blue"
    13. }
    14. Rectangle {
    15. id: member2
    16. height: delegateRectangle.height * 20 / 100
    17. width: delegateRectangle.width
    18. anchors.top: member1.bottom
    19. anchors.left: parent.left
    20. color: "gray"
    21. }
    22. Rectangle {
    23. id: member3
    24. height: delegateRectangle.height * 20 / 100
    25. width: delegateRectangle.width
    26. anchors.top: member2.bottom
    27. anchors.left: parent.left
    28. color: "red"
    29. }
    30. Rectangle {
    31. id: member4
    32. height: delegateRectangle.height * 20 / 100
    33. width: delegateRectangle.width
    34. anchors.top: member3.bottom
    35. anchors.left: parent.left
    36. color: "yellow"
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

    I want to use percentages inside a delegate so it can easily resize when window size changes, but after seeing that the total percentages was bellow 100% and elements went outside of boundaries of the view I decided to make a basic example with rectangles and as you can see in the attached image the member4 rectangle goes outside of boundaries.
    Can anyone explain me why is that? I don't see what it's missing.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Misscalculations when calculating percentages inside Rectangle

    You could be hitting rounding errors.

    If there is no scaling then 1 unit is one pixel, if the height calculation results in non-integer values, there will be rounding, which can add up.

    Cheers,
    _

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

    adutzu89 (17th December 2016)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Misscalculations when calculating percentages inside Rectangle

    You could be hitting rounding errors.
    If I'm reading the code correctly, each rectangle is 20% of the height of the parent rectangle. There should be enough room for 5 of them. A 1-pixel rounding error could not possibly cause the effect shown in the figure with only 4 rectangles.

    @adutzu89: Are you sure the code you posted matches the figure?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #4
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Misscalculations when calculating percentages inside Rectangle

    Quote Originally Posted by d_stranz View Post
    If I'm reading the code correctly, each rectangle is 20% of the height of the parent rectangle. There should be enough room for 5 of them. A 1-pixel rounding error could not possibly cause the effect shown in the figure with only 4 rectangles.

    @adutzu89: Are you sure the code you posted matches the figure?
    Hi d_stranz,

    Yes the code is correct, that's why I felt the need to ask it here, I found it odd.
    I adjusted it properly with setting the items other values, I am thinking that it the size of the list is returned as list + list header(even tough I haven't set any header).

Similar Threads

  1. Replies: 1
    Last Post: 15th June 2016, 08:51
  2. calculating the Rotation of Images
    By 2lights in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2013, 11:28
  3. Calculating button width
    By Skizmo in forum Qt Programming
    Replies: 6
    Last Post: 17th February 2011, 15:12
  4. Calculating best bounding box for text
    By xtal256 in forum Qt Programming
    Replies: 2
    Last Post: 1st October 2010, 07:38
  5. calculating checksum in serialcommunication
    By jjbabu in forum Qt Programming
    Replies: 5
    Last Post: 12th October 2007, 12:43

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.