[SOLVED]save a png file



  • Hello,

    In my application I create a png file and save it in a directory (named result.png). I want to get this image without using a dialog box from this directory and then save it in an other directory using a dialog box.How could this be done? I hope someone could help.


  • Moderators

    a combination of "QFile :: copy":http://doc.qt.nokia.com/4.7/qfile.html#copy and "QFileDialog ":http://doc.qt.nokia.com/4.7/qfiledialog.html will do the trick.



  • So let me see if I got this correct. You create an Image and save it in some directory as result.png and then you want to copy it to another directory, while the user can choose the directory with a file dialog ("QFileDialog::getExistingDirectory":http://doc.qt.nokia.com/4.7/qfiledialog.html#getExistingDirectory). To copy, you can either use QFile::copy or just open one file, read it into a QByteArray (I would suggest to adjust the size of the read depending on memory and harddrive sector size) and write that back to a different file.



  • To copy an image from one directory to another should be something like the following code. But I want to copy two files for 3D images (.hdr, .img), which are located in the same directory. How I could copy these both two image files in an other directory by using the same QFileDialog for saving images?

    @void MainWindow::save()
    {

    QString newName = QFileDialog::getSaveFileName(
    this,
    tr("Save image"),
    QDir::currentPath(),
    tr("Image (*.img)") );

    QFile::copy("result.img",newName);

    }@



  • Wild guess:
    @void MainWindow::save()
    {

    QString newName = QFileDialog::getSaveFileName(
    this,
    tr("Save image"),
    QDir::currentPath(),
    tr("Image (*.img)") );

    QFile::copy("result.img",newName);
    newName.replace(".img",".hdr");
    QFile::copy("result.hdr",newName);
    }@



  • I have tried this and I get my results. But with this method I open a QFileDialog twice. Is there any method to do the same thing just using QFileDialog once?

    @QString newName_hdr = QFileDialog::getSaveFileName(
    this,
    tr("Save image"),
    QDir::currentPath(),
    tr("Image (*.hdr)") );

    QString newName_img = QFileDialog::getSaveFileName(
    this,
    tr("Save image"),
    QDir::currentPath(),
    tr("Image (*.img)") );

    QFile::copy("result.hdr",newName_hdr);
    QFile::copy("result.img",newName_img);@



  • Thanks kkrzewniak,

    It was exactly what I was looking for. The problem is solved.


Log in to reply
 

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