Can't save an image from a QLabel to disc
-
Hi, I am trying to convert a QPixmap to a QImage from a QLabel named label2 , and then save the image .
QImage image;
QString imagePath = QFileDialog::getSaveFileName( this,tr("Save File"),"", tr("JPEG (*.jpg .jpeg);;PNG (.png)" ));QPixmap pixmap = ui->label2->pixmap(); if (!pixmap.isNull() ) { image =pixmap.toImage() ; } image.save(imagePath);
This is what I've tried but it's not working, it doesn't save the image. Do you know why?
-
@georgiav said in Can't save an image from a QLabel to disc:
but it's not working, it doesn't save the image.
Please be more specific - is a file generated? Is imagePath valid (with a proper extension)? Is pixmap not null? Simply add some debug output to see what exactly does not work.
-
@georgiav said in Can't save an image from a QLabel to disc:
way to get a QPixmap from a QLabel?
When you set a pixmap on the label previously, yes.
-
so I changed it to this:
if (!pm.isNull() && !imagePath.isEmpty() ) { cout<<"rfsd"; image =pm.toImage() ; image.save(imagePath); }
and it prints rfsd but doesn't save the image.
-
@Christian-Ehrlicher said in Can't save an image from a QLabel to disc:
Is imagePath valid (with a proper extension)?
do you have write access in the selected given directory?
-
@Christian-Ehrlicher could you please tell me how can I check that?
-
Hi,
Go to that folder and try to create a file for example.
-
Either ensure your file name ends with the proper extension or add the format to use in the save call.
-
@SGaist Okay, and how can I update this to save the extension?
QString imagePath = QFileDialog::getSaveFileName( this,tr("Save File"),"", tr("JPEG (*.jpg .jpeg);;PNG (.png)" ));
if I'd like the user to be able to choose if the image will be JPEG or PNG
-
Use the selectedFilter output parameter to get the information.