0

I have this code, that works

 QFile source(sourceFile);
    source.open(QIODevice::ReadOnly);
    QFile destination(newName);
    destination.open(QIODevice::WriteOnly);
    QByteArray buffer;
    int chunksize = 200;

    while(!(buffer = source.read(chunksize)).isEmpty())
    {
        destination.write(buffer);
    }
    destination.close();
    source.close();

What chunksize should I use, if I want to copy big files (2-10 GB) ? Is this code efficient, or it should be better than just copying chunks.

dsfdf
  • 13
  • 3
  • Is there a reason you are not using QFile::copy() instead ? For example it will take care of file attributes for you. – Bogolt Jul 23 '17 at 14:42
  • I would like to use QFile::copy(), but I want to calculate progress of copied file, that is why I use chunks. Is there a way to calculate progress in percent using QFile::copy() ? – dsfdf Jul 23 '17 at 14:58
  • Use signal bytesWritten(qint64) http://doc.qt.io/qt-5/qiodevice.html#bytesWritten as it is QFile parent, you can do it easily. You'll have simple QFile::copy and callbacks telling how many bytes was already copied. – Bogolt Jul 23 '17 at 15:02
  • If you want to report progress the only way to do it is not **not** block the main thread so your gui can refresh. Meaning you have to use an async worker as described in the linked question. – dtech Jul 23 '17 at 15:35

0 Answers0