Results 1 to 3 of 3

Thread: Using negative values for the x and y args to QRectF when calling Qpainter.drawElipse

  1. #1
    Join Date
    Nov 2020
    Location
    Syracuse NY
    Posts
    4
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Using negative values for the x and y args to QRectF when calling Qpainter.drawElipse

    What is the purpose of passing negative values to the QRect constructor for the x and y parameters?

    Example:

    Qt Code:
    1. QPainter painter(this);
    2. painter.setRenderHint(QPainter::Antialiasing, antialiased);
    3. painter.translate(width() / 2, height() / 2);
    4.  
    5. for (int diameter = 0; diameter < 256; diameter += 9)
    6. {
    7. painter.drawEllipse(QRectF(-diameter / 2.0, -diameter / 2.0, diameter, diameter));
    8. }
    To copy to clipboard, switch view to plain text mode 

    The code above draws a series of concentric circles centered at a fixed point. If I change to positive values, the center of each circle moves, drawing a 'cone effect' on the screen.



    My question is what does negating the x and y arguments really do?
    I have searched a lot for this topic but nothing comes up that really answers my question.

    Thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,842
    Thanks
    270
    Thanked 786 Times in 776 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Using negative values for the x and y args to QRectF when calling Qpainter.drawEl

    My question is what does negating the x and y arguments really do?
    The center of the circle drawn by drawEllipse() is the center of rectangle you give it as argument. So if you want every circle drawn around the same center, then you have to use that same center point every time. The rectangle is defined by its top left (x,y) origin, width and height, not its center. So if you increase the diameter of the circle without moving the top left corner of the rectangle to compensate, then the center of the rectangle will move to the right with each call, and your circles will make a cone shape.

    By subtracting half the diameter from x and y with each pass through the loop, you are giving it the same center each time so the circles remain concentric. You can easily confirm this - make the QRectF a local variable inside the loop and print out its center() using qDebug() in the debugger.
    <=== 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.

  3. #3
    Join Date
    Nov 2020
    Location
    Syracuse NY
    Posts
    4
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Using negative values for the x and y args to QRectF when calling Qpainter.drawEl

    Ah, ok, that explains it. Yes, I first thought the x and y to QRectF was the center of the circle, not its top-left corner. Thanks!

Similar Threads

  1. Replies: 2
    Last Post: 29th January 2020, 08:17
  2. qwtplot drawing lines with negative values
    By dimbor2708 in forum Qwt
    Replies: 2
    Last Post: 14th April 2017, 15:00
  3. Replies: 1
    Last Post: 24th November 2016, 09:04
  4. How to use main args
    By roseicollis in forum Newbie
    Replies: 4
    Last Post: 23rd December 2014, 13:36
  5. va_start used in function with fixed args
    By stinos in forum General Programming
    Replies: 5
    Last Post: 28th November 2008, 01:49

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.