Results 1 to 3 of 3

Thread: QPainter::save and QPAinter::restore()

  1. #1
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default QPainter::save and QPAinter::restore()

    In doc it says QPainter::save() saver the curent state of painter and restore do restore(). I couldn't find out what it mean state of QPainter. I tried without them also i got output.

    What is the significance of these 2 function and state of a painter.

  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: QPainter::save and QPAinter::restore()

    It's really usefull if you want to modify some of QPainter's attributes for a short while and then set it back. For example imagine that you want to draw a part of the "image" rotated, with a different font and colour. Without QPainter::save/restore you'd need to do:

    Qt Code:
    1. painter->rotate(90);
    2. QFont oldfont = painter->font();
    3. QPen oldpen = painter->pen();
    4. painter->setFont(QFont(...));
    5. painter->setPen(QPen(...));
    6. painter->drawText(...);
    7. painter->setFont(oldfont);
    8. painter->setPen(oldpen);
    9. painter->rotate(-90);
    To copy to clipboard, switch view to plain text mode 

    The same thing with save and restore:
    Qt Code:
    1. painter->save();
    2. painter->rotate(90);
    3. painter->setFont(QFont(...));
    4. painter->setPen(QPen(...));
    5. painter->drawText(...);
    6. painter->restore();
    To copy to clipboard, switch view to plain text mode 

    This example is simple but imagine you have to do more transformations and them restore the state of the painter before the operation, because something else will want to paint on it too and expects some "default" settings. Save/restore pair is very handy here. I think it came from OpenGL world where you can save/restore the state of matrices.

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

    quickNitin (18th June 2006)

  4. #3
    Join Date
    May 2006
    Location
    Bangalore
    Posts
    23
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Angry Re: QPainter::save and QPAinter::restore()

    save() and restore() are particularly useful for building hierarichal models.

    For example a robot:
    Qt Code:
    1. painter->save();
    2.  
    3. translate, scale, or rotate the torso;
    4. draw the torso;
    5.  
    6. painter->save();
    7. translate, scale, or rotate the left hand;
    8. draw the left hand;
    9. painter->restore();
    10.  
    11. painter->save();
    12. translate, scale, or rotate the right hand;
    13. draw the right hand;
    14. painter->restore();
    15.  
    16. painter->restore();
    To copy to clipboard, switch view to plain text mode 

    The right hand has nothing to do with the left hand's transformations. If either hands move, nothing else changes; However if the torso moves, both the hands move as well.

    Quote Originally Posted by wysota
    I think it came from OpenGL world where you can save/restore the state of matrices.
    As Wysota said, we use such a concept widely in OpenGL programming.
    *OpenGL's equivalent is glPushMatrix() and glPopMatrix()
    The Keyboard Is Mightier Than The Sword!

  5. The following user says thank you to bits for this useful post:

    quickNitin (18th June 2006)

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.