add progress bar for QCryptographicHash::MD5 for files
-
@JonB
i write this code based of your code .
its work without problem
do you think it better then the code i post in above?QString Md5_gen(QString const &fname, QString const &hash) { QString pakchunk_Md5; QCryptographicHash crypto(QCryptographicHash::Md5); QFile pakchunk(fname); qint64 fSize = pakchunk.size(); if (pakchunk.open(QIODevice::ReadOnly)) { char buf[8192]; int bytesRead; qint64 overallBytesRead = 0; qint64 percentProgress = 0; while((bytesRead = pakchunk.read(buf, 8192)) > 0){ crypto.addData(buf, bytesRead); overallBytesRead += bytesRead; percentProgress = overallBytesRead * 100 / fSize; } pakchunk_Md5 = crypto.result().toHex(); } else { qDebug() << "Can't open file."; pakchunk_Md5 = "nofile"; return pakchunk_Md5; } return pakchunk_Md5 }
using this code my cpu usage jump to 12% (from ~3%)
there is anyway to reduce cpu usage? -
@saeid0034
The code is closer to what I wrote, butUntil you define some signal and have your file reading loop emit that signal, with a parameter to say how far it has reached, and have that signal connected to a slot which accepts that parameter and updates the progressbar value, you won't be getting anywhere toward your progress goal.
You wrote
there is anyway to reduce cpu usage?
Concentrate on one question at a time. You wanted a progressbar.
-
QThread *t = new QThread(this); HashChecker *hc = new HashChecker(); hc->moveToThread(t); QObject::connect(t, &QThread::started, hc, &HashChecker::check_sequential); QObject::connect(hc, &HashChecker::doneFile, this, &MainWindow2::onHashCalculated); QObject::connect(hc, &HashChecker::finished, t, &QThread::quit); QObject::connect(hc, &HashChecker::fileReadProgress, ui->progressBar, &QProgressBar::setValue); t->start();
i move all my Md5 process to another thread, then start it
after that im getting fileReadProgress
and Ok, i create another Question for my other question -
You leak the HashChecker instance.
-
@Christian-Ehrlicher said in add progress bar for QCryptographicHash::MD5 for files:
You leak the HashChecker instance.
what can i do about this?
-
@saeid0034 said in add progress bar for QCryptographicHash::MD5 for files:
what can i do about this?
Properly clean it up after you finished the calculation maybe?
-
This post is deleted!
-
@saeid0034
That doesn't sound right, @Christian-Ehrlicher is referring to yourHashChecker *hc = new HashChecker();
heap allocation needing to bedelete
d.