June 29, 2011

jk_mk jk_mk
Lab Rat
108 posts

[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.

6 replies

June 29, 2011

koahnig koahnig
Dinosaur Breeder
2908 posts

a combination of QFile :: copy [doc.qt.nokia.com] and QFileDialog [doc.qt.nokia.com] will do the trick.

June 29, 2011

loladiro loladiro
Lab Rat
596 posts

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 [doc.qt.nokia.com]). 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.

July 2, 2011

jk_mk jk_mk
Lab Rat
108 posts

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?

  1. void MainWindow::save()
  2. {
  3.  
  4.  QString newName = QFileDialog::getSaveFileName(
  5.         this,
  6.         tr("Save image"),
  7.         QDir::currentPath(),
  8.         tr("Image (*.img)") );
  9.  
  10.  QFile::copy("result.img",newName);
  11.  
  12. }

July 2, 2011

kkrzewniak kkrzewniak
Lab Rat
219 posts

Wild guess:

  1. void MainWindow::save()
  2. {
  3.  
  4.  QString newName = QFileDialog::getSaveFileName(
  5.         this,
  6.         tr("Save image"),
  7.         QDir::currentPath(),
  8.         tr("Image (*.img)") );
  9.  
  10.  QFile::copy("result.img",newName);
  11.  newName.replace(".img",".hdr");
  12.  QFile::copy("result.hdr",newName);
  13. }

 Signature 

Me, Grimlock, not “nice dino”. ME BASH BRAINS!

July 2, 2011

jk_mk jk_mk
Lab Rat
108 posts

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?

  1. QString newName_hdr = QFileDialog::getSaveFileName(
  2.         this,
  3.         tr("Save image"),
  4.         QDir::currentPath(),
  5.         tr("Image (*.hdr)") );
  6.  
  7.  QString newName_img = QFileDialog::getSaveFileName(
  8.         this,
  9.         tr("Save image"),
  10.         QDir::currentPath(),
  11.         tr("Image (*.img)") );
  12.  
  13.  QFile::copy("result.hdr",newName_hdr);
  14.  QFile::copy("result.img",newName_img);

July 2, 2011

jk_mk jk_mk
Lab Rat
108 posts

Thanks kkrzewniak,

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

 
  ‹‹ Font for all widget      QProgressbar text within QProgressbar ››

You must log in to post a reply. Not a member yet? Register here!