Saving an image in a file (SOLVED)

  • Hello everyone.
    I am new to Qt and hence am trying to learn it through working with documented examples.
    Currently I am working om the Image viewer example.
    I wanted to add a Save function alongwith the Open function already given...Here is what I have done:

    void ImageViewer::save()
    {QString fileName=QFileDialog::getSaveFileName(this,tr("Save File"),QDir::currentPath());
    QPixmap pixmap(*imageLabel->pixmap());
    QFile f(fileName);;;

    But the program produces the following error on compilation:
    "no matching for call to QPixmap::save(QFile&, const char[4])"
    before I added the code fragment that actually accepted the image to save...the program sequence worked fine and it also saved an empty image in the desired location.
    So can anyone help me out here as in what would be the required coding for the same.

  • Moderators


    The functions signature doesnot matches as that of yours.
    As the compile output says you need to pass filename and the format.
    See "QPixmap::save(const QString & fileName, const char * format = 0, int quality = -1)": for more details.

  • Okay, I now changed the coding according to what you it is:
    void ImageViewer::savef()
    QString fileName=QFileDialog::getSaveFileName(this,tr("Save File"),QDir::currentPath());
    QPixmap pixmap(*imageLabel->pixmap());
    QFile f(fileName);;,"JPG");
    Now the error says filename was not declared in this scope. What is the problem now? I think I have declared it above in the function.

  • Sorry, my mistake...the coding is just fine...please don't worry to reply. Thanks a lot for your help...;)

  • Moderators

    Well just to remind, if you think that this thread is solved then mark it by editing the title and prepend [solved] ;)

  • OK! (y)

  • Hello, Ijust want to tell you that, don't need : QFile f(fileName);;f.close();

  • Moderators

    Yes ofcourse it was not needed.

  • yea I see that. thanks ;)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.