Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainter - save and restore
Forum Updated to NodeBB v4.3 + New Features

QPainter - save and restore

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qwe3
    wrote on last edited by qwe3
    #1

    Hi,

    I find this topic in stack "What does QPainter::save () and QPainter::restore () do?" . One user wrote:

    https://stackoverflow.com/questions/3016709/what-does-qpaintersave-and-qpainterrestore-do
    
    As you are probably changing the color and style or any other settings of the paint you usually want to exit your paint function with the same settings that it had when coming in. Therefore you use QPainter::save() before a change in painter settings and QPainter::restore() after you are done drawing with your changed settings e.g.
    

    I check it:

    void MainWindow::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        static int i=0;
        if(i==0)
        {
            painter.setPen(Qt::red);
        }
        painter.drawEllipse(QPoint(100,100),10,10);
        i++;
    }
    

    And... when I go outside paintEvent function, the settings, which I changed, are reset to default ( I change pen to red, and when I go in paintEvent for the second time I still have default settings and draw with black color ). So why this user wrote that?

    I thought that restore and save funtions are only to help us in this situation:

        QPainter painter(this);
        painter.save();
        painter.setPen(Qt::red);
        ....  and many, many other change settings
        painter.restore();
        painter.drawEllipse(QPoint(100,100),10,10);    // <- here we have to draw this using default settings
    
    JonBJ 1 Reply Last reply
    0
    • Q qwe3

      Hi,

      I find this topic in stack "What does QPainter::save () and QPainter::restore () do?" . One user wrote:

      https://stackoverflow.com/questions/3016709/what-does-qpaintersave-and-qpainterrestore-do
      
      As you are probably changing the color and style or any other settings of the paint you usually want to exit your paint function with the same settings that it had when coming in. Therefore you use QPainter::save() before a change in painter settings and QPainter::restore() after you are done drawing with your changed settings e.g.
      

      I check it:

      void MainWindow::paintEvent(QPaintEvent *event)
      {
          QPainter painter(this);
          static int i=0;
          if(i==0)
          {
              painter.setPen(Qt::red);
          }
          painter.drawEllipse(QPoint(100,100),10,10);
          i++;
      }
      

      And... when I go outside paintEvent function, the settings, which I changed, are reset to default ( I change pen to red, and when I go in paintEvent for the second time I still have default settings and draw with black color ). So why this user wrote that?

      I thought that restore and save funtions are only to help us in this situation:

          QPainter painter(this);
          painter.save();
          painter.setPen(Qt::red);
          ....  and many, many other change settings
          painter.restore();
          painter.drawEllipse(QPoint(100,100),10,10);    // <- here we have to draw this using default settings
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @qwe3
      The QPainter painter goes out of scope at the end of the function and is destroyed. There isn't some kind of "global state" it has permanently changed.

      The save/restore() is for use inside the scope to undo changes, as your second example shows.

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved