Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Performance problem with QFile.



  • I'm trying to calculate the hash of some disk image files (.iso)
    so i have to read the entire file and store it in QByteArray to pass it later to QCryptographicHash::Hash
    the problem is with reading performance, it takes a lot of time and the system freeze while using QFile::readAll() and storing it in byte array.
    is there any solution to reduce the time and prevent the system form freezing?


  • Moderators

    Use the stream operators. QCryptographicHash has an API that works with streaming QIODevices, too.



  • can you write a small piece of code :)



  • Try like this:

    @/max bytes to read at once/
    static const qint64 CHUNK_SIZE = 4096;

    /init hash/
    QCryptographicHash hash(Sha1);

    /open file/
    QFile file("foo.bar");
    if (!file.open(QIODevice::ReadOnly))
    return;

    /process file contents/
    QByteArray temp = file.read(CHUNK_SIZE);
    while(!temp.isEmpty())
    {
    hash.addData(temp);
    temp = file.read(CHUNK_SIZE);
    }

    /finalize/
    const QByteArray res = hash.result();
    qDebug("Hash is: %s", res.toHex());@



  • thank you, now it works times better :)


Log in to reply