add progress bar for QCryptographicHash::MD5 for files
-
Hi I use this code for generate MD5 hash of files
QString Md5_gen(QString const &s) { QString pakchunk_Md5; QCryptographicHash crypto(QCryptographicHash::Md5); QFile pakchunk(s); if (pakchunk.open(QIODevice::ReadOnly)) { while(!pakchunk.atEnd()){ crypto.addData(pakchunk.read(8192)); } } else { qDebug() << "Can't open file."; pakchunk_Md5 = "nofile"; return pakchunk_Md5; } pakchunk_Md5 = crypto.result().toHex(); return pakchunk_Md5;
some of files are big and take some time for QCryptographicHash to generate MD5 hash of them, Im looking for a way to add a progress bar for check Progress of MD5 generating
is it possible? (i also use signal and slot with QFuture, QFutureWatcher and QtConcurrent) -
@saeid0034 said in add progress bar for QCryptographicHash::MD5 for files:
auto remains = pakchunk.bytesAvailable(); auto progress = ((size - remains) * 100) / size;
This just sets two local variables, which are thrown away. Come on, you need to do a bit more than that! Algorithmically (no, I'm not going to type in the exact C++ statements)
// In Md5_gen SIGNAL fileReadProgress(int percentProgress) const chunkSize = 8192 file = QFile(filename) fileSize = file.fileSize() readSoFar = 0 while ((readNow = file.read(buffer, chunkSize)) > 0) crypto.addData(buffer, readNow) readSofar += readNow percentProgress = readSoFar * 100 / fileSize emit fileReadProgress(percentProgress) // In outside world, where you know about the progressbar widget/dialog connect(Md5_genObject, &Md5_gen::fileReadProgress, progressbarObject, &Progressbar::setValue)
Until 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.
P.S.
BTW, yourQString Md5_gen(QString const &s)
looks like a global function. You must move this into/write a wrapper class --- derived fromQObject
--- before you can define any Qt signals and emit them from it. -
emit a signal, connected to progress control, using value generated like:
const auto result = pakchunk.size()/(chunksize*incrementalCounter)
https://doc.qt.io/qt-5/qfile.html#size divided by https://doc.qt.io/qt-5/qiodevice.html#read-1
? -
@saeid0034 said in add progress bar for QCryptographicHash::MD5 for files:
is it possible?
Yes, emit a signal each time new file is handled and in a slot connected to that signal update the progress bar
-
@jsulm thanks, i don't know i understand you right or not
im want to update progress bar per each file, i mean real md5 generation process of each file show in a progress bar but i don't know how to get progress value
there is any Close example for what im looking for?@6thC thanks,
const auto result = pakchunk.size()/(chunksize*incrementalCounter)
show progress of reading the file? -
@saeid0034 said in add progress bar for QCryptographicHash::MD5 for files:
@6thC thanks, const auto result = pakchunk.size()/(chunksize*incrementalCounter) show progress of reading the file?
Well, yes, but you have to implement that (e.g. you need to do the
incrementalCounter
), and play with the code a tiny bit. The point being: if you know how big the whole file is, and you know how far you have read through it in a loop, you are ready to output a progress percentage. -
@saeid0034 said in add progress bar for QCryptographicHash::MD5 for files:
anyone have any code example for do this job
Which job exactly? How to get file size?
-
@jsulm as i said i want to have a progress bar for MD5 Generation Progress (for each file)
and for get file size maybe something like thisQFile pakchunk(s);; if (pakchunk.open(QIODevice::ReadOnly)){ int pakchunk_size = myFile.size(); //when file does open. pakchunk.close(); }
-
@saeid0034
We have given you the algorithm, you just need to write the few lines of code.You had
while(!pakchunk.atEnd()){ crypto.addData(pakchunk.read(8192)); }
Just add the line in the loop to emit a signal with a parameter of an integer in the range 0--100 representing how far through the file you have read.
-
i get what i need to do
only one problem
i have Md5_gen code in main.cpp
how can i emit in main.cpp and get it in mainwindow2.cpp?main.cpp
QString Md5_gen(QString const &s) { QString pakchunk_Md5; QCryptographicHash crypto(QCryptographicHash::Md5); QFile pakchunk(s); if (pakchunk.open(QIODevice::ReadOnly)) { while(!pakchunk.atEnd()){ crypto.addData(pakchunk.read(8192)); } } else { qDebug() << "Can't open file."; pakchunk_Md5 = "nofile"; return pakchunk_Md5; } pakchunk_Md5 = crypto.result().toHex(); return pakchunk_Md5; } int main(int argc, char *argv[]) { QApplication a(argc, argv); mainwindow w; w.show(); return a.exec(); }
Also i do this for get progressbar value
QString Md5_gen(QString const &s) { QString pakchunk_Md5; QCryptographicHash crypto(QCryptographicHash::Md5); QFile pakchunk(s); if (pakchunk.open(QIODevice::ReadOnly)) { const auto size = pakchunk.size(); while(!pakchunk.atEnd()){ crypto.addData(pakchunk.read(8192)); auto remains = pakchunk.bytesAvailable(); auto progress = ((size - remains) * 100) / size; } } else { qDebug() << "Can't open file."; pakchunk_Md5 = "nofile"; return pakchunk_Md5; } pakchunk_Md5 = crypto.result().toHex(); return pakchunk_Md5; }
in case someone need it
-
@saeid0034 said in add progress bar for QCryptographicHash::MD5 for files:
how can i emit in main.cpp and get it in mainwindow2.cpp?
I don't understand your code: who calls Md5_gen and where? Just add Md5_gen to the class where you calculate the checksum...
-
@saeid0034 said in add progress bar for QCryptographicHash::MD5 for files:
auto remains = pakchunk.bytesAvailable(); auto progress = ((size - remains) * 100) / size;
This just sets two local variables, which are thrown away. Come on, you need to do a bit more than that! Algorithmically (no, I'm not going to type in the exact C++ statements)
// In Md5_gen SIGNAL fileReadProgress(int percentProgress) const chunkSize = 8192 file = QFile(filename) fileSize = file.fileSize() readSoFar = 0 while ((readNow = file.read(buffer, chunkSize)) > 0) crypto.addData(buffer, readNow) readSofar += readNow percentProgress = readSoFar * 100 / fileSize emit fileReadProgress(percentProgress) // In outside world, where you know about the progressbar widget/dialog connect(Md5_genObject, &Md5_gen::fileReadProgress, progressbarObject, &Progressbar::setValue)
Until 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.
P.S.
BTW, yourQString Md5_gen(QString const &s)
looks like a global function. You must move this into/write a wrapper class --- derived fromQObject
--- before you can define any Qt signals and emit them from it. -
@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!