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

Save a QPixmap in a label



  • Hello,

    I would like to save my picture (QPixmap) as a png in my label, when I finished to add lines and ellipses.
    Like describe in the commentaries:

    @
    //---------------------------------------------Class LabelImage
    class LabelImage: public QLabel
    {
    public:

    LabelImage()
    {
      this->setMouseTracking(true);
    };
    
    void paintEvent (QPaintEvent *event)
    {
         QLabel::paintEvent( event );
         QPainter painter(this);
             
              // LINES ANS ELLIPSES TREATMENT LOOPS
    
             // BACK UP HERE ! like:
             //monPixmap.save("/Users/.../Desktop/GUI Picture/Picture " + 
             //QString::number(NumberOfPicture) +".png","png",100);
                             
                    MesureFinished = true;
    }
    
    void mousePressEvent (QMouseEvent * event)
    {
        if (MesureFinished == false)
        {
            PixelColumn = event->x();
            PixelRow = event->y();
            this->update();
            QLabel::mousePressEvent( event );
        }
    };
    

    };
    @



  • and here the c++ code where I'm using the .h:
    @
    //---------------------------------------------PICTURE WINDOW
    PictureWindow::PictureWindow(QImage Image, int NumberOfPicture) : QWidget()
    {
    setFixedSize(840, 356);

    QPixmap monPixmap;
    
    LabelImage* LabelIMG = new LabelImage();
    
    LabelIMG-> setFixedSize(monPixmap.fromImage(Image).size());
    

    }
    @



  • No way to go. You have to draw in a QPixmap instead by doing something like:
    @QPainter p(pixmap);@
    Don't do it inside paintEvent.

    Then you can save the pixmap and display it in a QWidget inside paintEvent using painter.drawPixmap(). No need to subclass QLabel, just subclass QWidget.


  • Qt Champions 2017

    I'm assuming that you have picture in png format. You need to load this in pixmap and set this to label. Is that correct ?

    1. Load the your png file to PixMap
    2. Set this PixMap to QLabel

    Subclassing QLabel and implementing paintEvent will re-draw you label.



  • Try this..
    QString format = "png";

    QString fileName=CurrentDirPath+"/"+"image.png"; 
    
    QPixmap originalPixmap; 
    
    originalPixmap=QPixmap::grabWidget(label);
    
    
    if (!fileName.isEmpty())
    
        originalPixmap.save(fileName, format.toAscii());
    


  • Thanks all for those answers,

    [quote author="Dheerendra" date="1389809506"]I'm assuming that you have picture in png format. You need to load this in pixmap and set this to label. Is that correct ?

    1. Load the your png file to PixMap
    2. Set this PixMap to QLabel

    Subclassing QLabel and implementing paintEvent will re-draw you label.

    [/quote]

    I now back up the picture without figures (circles and ellipses) with this code:

    @

    //---------------------------------------------Class LabelImage
    class LabelImage: public QLabel
    {
    QPixmap mypix; //----------------------------------------------------NEW
    public:

    LabelImage()
    {
      mypix = ("..."); //---------------------NEW
      this->setPixmap(mypix);//---------------------NEW
    
      this->setMouseTracking(true);
    };
    
    void paintEvent (QPaintEvent *event)
    {
         QLabel::paintEvent( event );
         QPainter painter(this);
             
              // LINES ANS ELLIPSES TREATMENT LOOPS
                             
                    MesureFinished = true;
                    mypix.save(".."); //--------------------------------------NEW
    }
    
    void mousePressEvent (QMouseEvent * event)
    {
        if (MesureFinished == false)
        {
            PixelColumn = event->x();
            PixelRow = event->y();
            this->update();
            QLabel::mousePressEvent( event );
        }
    };
    

    };
    @

    How I can save picture + figures?


  • Lifetime Qt Champion

    Hi,

    Are you trying to create something like a Paint program ?



  • Hi SGaist,

    Yes something like that


  • Lifetime Qt Champion

    Then have a look at the Qt's Plug And Paint example, that will provide you a good ground to build on



  • Ok I will check. Thank you


Log in to reply