I made a simple image viewer, now how do i save that image with save and also save as ? Can you paste me some code example how to do that.
I have read the image with:
Code:
if (!fileName.isEmpty()) { if (image.isNull()) { return; }
thx.
Printable View
I made a simple image viewer, now how do i save that image with save and also save as ? Can you paste me some code example how to do that.
I have read the image with:
Code:
if (!fileName.isEmpty()) { if (image.isNull()) { return; }
thx.
For saving as, check out: QFileDialog::getSaveFileName()
For saving QImage to a file: QImage::save()
Well yes, this opens me the dialog to enter filename. But how do i actualy get my image to save to that filename ?
To be more exact, done this:
Code:
void ImageViewer::save() { QString s = QFileDialog::getSaveFileName(this,"Choose a filename to save under",QDir::currentPath(),"Images (*.png *.xpm *.jpg)"); QByteArray ba; image.save(&buffer, "JPG"); }
doesnt save nothing as i specify :)
What have i missunderstood ? :)
This is something I wrote a while back, maybe you'll find it usable
Code:
void ImageViewer::saveFileAs() { // construct a filter of all supported formats QString filter; { } // remove unnecessary chars from the end of the filter if (filter.endsWith(";;")) { filter.chop(2); } // get save file name QString selectedFilter; "Save image as", path, filter, &selectedFilter); if (!fileName.isEmpty()) { // keep track of the current path // check for the selected format if (!fi.suffix().endsWith(format, Qt::CaseInsensitive)) { // remove possible incorrect suffix fileName.chop(fi.suffix().length()); // set correct suffix fileName += "." + format.toLower(); } // save image in the selected format if (!image.save(fileName, format.toAscii().constData())) { } } }
Assuming that you want to save the shown image, put the "QImage image" as a member variable and use that same variable for loading and saving.Quote:
Originally Posted by Godlike
Realy a newbie at this but i cant get it to work, thanx for sharing this code first. I made it this far, i always get unable to save image (path is ok to where it wants to save and extension). My code till now:
Code:
void ImageViewer::save() { // construct a filter of all supported formats QString filter; QImage image; { } // remove unnecessary chars from the end of the filter if (filter.endsWith(";;")) { filter.chop(2); } // get save file name QString selectedFilter; QString s = QFileDialog::getSaveFileName(this, "Save image as", QDir::currentPath(), filter, &selectedFilter); if (!s.isEmpty()) { // check for the selected format if (!fi.suffix().endsWith(format, Qt::CaseInsensitive)) { // remove possible incorrect suffix s.chop(fi.suffix().length()); // set correct suffix s += "." + format.toLower(); } // save image in the selected format if (!image.save(s, format.toAscii().constData())) { } } }
I think you should actually use QFile if you want to write an image to a file instead of QBuffer.
The simplest way to save a QImage to a file should be something like this:
A more complicated but more flexible way:
Also remember to use "JPEG" not "JPG" for jpeg files.
This makes file great, but its empty ? All black and not saved what i have opened. You can see code how i opened file in my first post.
thx.
As I said, make the QImage image member variable.
If you instantiate a new QImage as a local variable in the function where you save, it's not the same image than you are viewing!
Then change the line 4 in your first post from:
to
Code:
image.load(fileName);
Load to and save the same image.
imageviewer.cpp:29: error: ‘image’ was not declared in this scope
imageviewer.cpp:35: error: no matching function for call to ‘QPixmap::fromImage(<type error>)’
/usr/include/QtGui/qpixmap.h:105: note: candidates are: static QPixmap QPixmap::fromImage(const QImage&, Qt::ImageConversionFlags)
make: *** [imageviewer.o] Error 1
if i change line 4 to what u told me :)
Oh and i would need to get that string fileName which is a name of that image then too to be visible in save function.
I just dont get it, simple as that :o
Did you declare a QImage member variable?Quote:
Originally Posted by Godlike
in imageviewer.h:
Edit: and the same thing goes for the QString fileName.. You know how to declare member variables, and what they are for, right? ;)
Ups you got me big time. Dunno why exactly needs to be there :p
But i put it there (probably wrong again :D ), and i get this error ... again :cool:
imageviewer.h:38: error: field ‘image’ has incomplete type
i put it in imageviewer.h so it is private and like this:
class ImageViewer : public QMainWindow
{
...
private:
QImage image;
QString fileName;
};
Please dont hate me ;)
Forget it was a stupid include missing ;) damn i forgot how to code it seems :D
And the silly thing works now ofcourse :) thanx for everything