Results 1 to 4 of 4

Thread: Problem understanding QPainter::setWindow (maybe)

  1. #1
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Problem understanding QPainter::setWindow (maybe)

    Hello everybody.

    i have a quite simple problem but couldn't get along with it.

    In the documentation, I've read that this code:

    Qt Code:
    1. QPainter p(this);
    2. ...
    3. p.setWindow(-50,-50,100,100);
    To copy to clipboard, switch view to plain text mode 

    would create a painting environment with (0, 0) as the center. But if I try to paint an ellipse which should fill out the widget completely with the following code:

    Qt Code:
    1. p.drawEllipse(-50,-50,100,100);
    To copy to clipboard, switch view to plain text mode 

    I get the following result:

    app.png

    So, obviously, the center is not (0, 0) but (-50, -50)?

    I if draw the ellipse with:
    Qt Code:
    1. p.drawEllipse(-100,-100,100,100);
    To copy to clipboard, switch view to plain text mode 
    I (almost) get the result i want to have:

    app2.png

    Well, this is not the coordinate system I expected. (0, 0) should be the center of my painting environment. What's my fault?

    Greetings,
    EMKAH

  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: Problem understanding QPainter::setWindow (maybe)

    Why do your drawings cover only part of the window? Do you have one widget embedded in another? Are you using layouts? How do you know the boundaries of the internal widget? The initial code is fine and should draw a perfect ellipse covering the whole area of the widget.
    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. #3
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Problem understanding QPainter::setWindow (maybe)

    Actually, i'm drawing on the widget itself. I've set the viewport to the largest square that fits into the widget:

    Qt Code:
    1. int _shortSide = qMin(width(), height());
    2. p.begin(this);
    3. p.setViewport((width() - _shortSide / 2), (height() - _shortSide / 2),
    4. _shortSide, _shortSide);
    5. p.setWindow(-50, -50, 100, 100);
    To copy to clipboard, switch view to plain text mode 

    The reason because I want to have 0,0 as the center of my coordinate system is that i want to perform a rotation around the center for further drawings.

  4. #4
    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: Problem understanding QPainter::setWindow (maybe)

    If the drawing is cut then your call to setViewport() is apparently invalid.

    However if you want to do rotations then I doubt setWindow is a good solution. It's easier to just translate the painter.

    Qt Code:
    1. void X::paintEvent(QPaintEvent *) {
    2. QPainter p(this);
    3. p.translate(width()/2, height()/2);
    4. p.drawEllipse(-50, -50, 100, 100);
    5. }
    To copy to clipboard, switch view to plain text mode 
    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.


Similar Threads

  1. Problem understanding the addressbook tutorial (part 3)
    By Blackened Justice in forum Newbie
    Replies: 6
    Last Post: 24th February 2012, 20:38
  2. understanding problem of threads, mutexes and waitconditions
    By Spooky in forum General Programming
    Replies: 0
    Last Post: 28th April 2011, 11:23
  3. Replies: 3
    Last Post: 18th July 2010, 13:53
  4. QPainter, setWindow, setViewport question
    By Micawber in forum Qt Programming
    Replies: 4
    Last Post: 3rd September 2007, 18:33
  5. Question on QPainter setWindow use
    By impeteperry in forum Qt Programming
    Replies: 14
    Last Post: 8th August 2006, 13:26

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.