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. Save the Qpainter with save() and restore()

Save the Qpainter with save() and restore()

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.5k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    MélissaQT
    wrote on last edited by
    #1

    Hello all,
    I have a problem on my qt project.
    I would like to save circles that I have created on an image, because for now I only have a new circle at every mouse click.
    Where I can place save() and restore()?

    My label is:
    @ void paintEvent (QPaintEvent *event)
    {
    QLabel::paintEvent( event );
    QPainter painter(this);
    painter.setPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::FlatCap, Qt::BevelJoin));

         RectImage.setX(PixelColumn-10);
         RectImage.setY(PixelRow-10);
         RectImage.setHeight(19);
         RectImage.setWidth(19);
         painter.drawEllipse(RectImage);
         
    }@
    

    Thanks,
    Mélissa

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      QPainter's save and restore are not meant for that. What you should do is keep a structure (e.g. a QVector) containing the circles data you want to draw. Each time you create a new circle, push it in the QVector and draw them all in your paintEvent.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        do like SGaist said.
        Or if you depend on this mechanismn you can also try QPicture class to save QPainter commands and replay them.

        QPainter's save()/restore() methods are just a stack to save the settings of the object.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MélissaQT
          wrote on last edited by
          #4

          Hello,
          thanks both for yours answers,
          I tried with a QVector of QlineF, now with line and no ellipse, like that:
          @
          extern std::vector<QLineF> LinesSaves;
          extern int NumLine;

          void paintEvent (QPaintEvent *event)
          {
          int CptLine;

               QLabel::paintEvent( event );
               QPainter painter(this);
          
               painter.setPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::FlatCap, Qt::BevelJoin));
          
               if (lineCreation)
               {
               Point1.setX(PixelColumn);
               Point1.setY(PixelRow);
               }
               else
               {
               Point2.setX(PixelColumn);
               Point2.setY(PixelRow);
          
               //problem below
               LinesSaves[NumLine].push_back(Point1,Point2);
               //problem above
          
               NumLine = NumLine +1;
               }
               for (int CptLine = 0; CptLine < NumLine; ++CptLine) {
                  painter.drawLine(LinesSaves[CptLine]);
               }
          
               lineCreation=!lineCreation;
          }@
          

          But, I have a problem where it's noted. It say:
          no member named 'push_back' in 'QLineF'
          I don't understand because I'm using the vector properties and not QLineF.

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            but int he line
            @
            LinesSaves[NumLine].push_back(Point1,Point2)
            @
            you are retrieving a QLineF object and calling puch_back() on it.

            Do this instead:
            @
            LinesSaves.push_back(Point1,Point2)
            @

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MélissaQT
              wrote on last edited by
              #6

              [quote author="raven-worx" date="1389260024"]but int he line
              @
              LinesSaves[NumLine].push_back(Point1,Point2)
              @
              you are retrieving a QLineF object and calling puch_back() on it.

              Do this instead:
              @
              LinesSaves.push_back(Point1,Point2)
              @[/quote]

              Yes it's logic, now I understand, thank you raven.

              1 Reply Last reply
              0

              • Login

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