Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Save the Qpainter with save() and restore()



  • 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


  • Lifetime Qt Champion

    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


  • Moderators

    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.



  • 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.


  • Moderators

    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 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.


Log in to reply