Leo
Leo

Reputation: 1139

Progress during a file saving

I have a large image that I have to save with image.save(fichier). How could I possibly display a progressbar of the saving state?

This may be a trivial question but I really don't how to do this as it's a one-line command...

Upvotes: 3

Views: 866

Answers (2)

Martin Beckett
Martin Beckett

Reputation: 96147

You probably can't using the built in image save. You could estimate the save time (know size of image, guess mb/s), and put up a progress bar and just use a timer to update it.

Or you can have a progress bar that just runs quickly to the end and restarts to show some activity but not the actual progress.

edit: if you really must have a progress you could 'save' the image data in memory and then write the memory to disk a block at a time updating the progress.

 QImage image;
 QByteArray bytes;
 QBuffer buffer(&bytes);
 buffer.open(QIODevice::WriteOnly);
 image.save(&buffer, "PNG"); // writes image into buffer in PNG format

Then write the buffer to disk

Upvotes: 1

Alessandro Pezzato
Alessandro Pezzato

Reputation: 8802

You can run QImage::save() in another thread, while the main thread shows the progressbar, updating its progress value every second with expected_size/current_size. You can get current size with QFile::size()

Upvotes: 1

Related Questions