Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. add progress bar for QCryptographicHash::MD5 for files
Forum Updated to NodeBB v4.3 + New Features

add progress bar for QCryptographicHash::MD5 for files

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 5 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 6thC6 Offline
    6thC6 Offline
    6thC
    wrote on last edited by
    #2

    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
    ?

    1 Reply Last reply
    1
    • S saeid0034

      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)

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      2
      • jsulmJ jsulm

        @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

        S Offline
        S Offline
        saeid0034
        wrote on last edited by
        #4

        @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?

        JonBJ 1 Reply Last reply
        0
        • S saeid0034

          @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?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #5

          @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.

          S 1 Reply Last reply
          1
          • JonBJ JonB

            @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.

            S Offline
            S Offline
            saeid0034
            wrote on last edited by saeid0034
            #6

            @JonB thanks, yes i know what is exact size of most of files, and for other file i think i need to just get file size first and then start generate Md5
            and anyone have any code example for do this job

            jsulmJ 1 Reply Last reply
            0
            • S saeid0034

              @JonB thanks, yes i know what is exact size of most of files, and for other file i think i need to just get file size first and then start generate Md5
              and anyone have any code example for do this job

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @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?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply
              1
              • jsulmJ jsulm

                @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?

                S Offline
                S Offline
                saeid0034
                wrote on last edited by
                #8

                @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 this

                QFile pakchunk(s);;
                if (pakchunk.open(QIODevice::ReadOnly)){
                    int pakchunk_size = myFile.size();  //when file does open.
                    pakchunk.close();
                } 
                
                JonBJ 1 Reply Last reply
                0
                • S saeid0034

                  @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 this

                  QFile pakchunk(s);;
                  if (pakchunk.open(QIODevice::ReadOnly)){
                      int pakchunk_size = myFile.size();  //when file does open.
                      pakchunk.close();
                  } 
                  
                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #9

                  @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.

                  1 Reply Last reply
                  2
                  • S Offline
                    S Offline
                    saeid0034
                    wrote on last edited by saeid0034
                    #10

                    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

                    jsulmJ JonBJ 2 Replies Last reply
                    0
                    • S saeid0034

                      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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @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...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • S saeid0034

                        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

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #12

                        @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, your QString Md5_gen(QString const &s) looks like a global function. You must move this into/write a wrapper class --- derived from QObject --- before you can define any Qt signals and emit them from it.

                        S 1 Reply Last reply
                        3
                        • JonBJ JonB

                          @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, your QString Md5_gen(QString const &s) looks like a global function. You must move this into/write a wrapper class --- derived from QObject --- before you can define any Qt signals and emit them from it.

                          S Offline
                          S Offline
                          saeid0034
                          wrote on last edited by saeid0034
                          #13

                          @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?

                          JonBJ 1 Reply Last reply
                          0
                          • S saeid0034

                            @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?

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by
                            #14

                            @saeid0034
                            The code is closer to what I wrote, but

                            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.

                            You wrote

                            there is anyway to reduce cpu usage?

                            Concentrate on one question at a time. You wanted a progressbar.

                            S 1 Reply Last reply
                            1
                            • JonBJ JonB

                              @saeid0034
                              The code is closer to what I wrote, but

                              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.

                              You wrote

                              there is anyway to reduce cpu usage?

                              Concentrate on one question at a time. You wanted a progressbar.

                              S Offline
                              S Offline
                              saeid0034
                              wrote on last edited by saeid0034
                              #15

                              @JonB

                                  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

                              1 Reply Last reply
                              1
                              • Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                You leak the HashChecker instance.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                S 1 Reply Last reply
                                2
                                • Christian EhrlicherC Christian Ehrlicher

                                  You leak the HashChecker instance.

                                  S Offline
                                  S Offline
                                  saeid0034
                                  wrote on last edited by
                                  #17

                                  @Christian-Ehrlicher said in add progress bar for QCryptographicHash::MD5 for files:

                                  You leak the HashChecker instance.

                                  what can i do about this?

                                  1 Reply Last reply
                                  0
                                  • Christian EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @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?

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    S 1 Reply Last reply
                                    1
                                    • Christian EhrlicherC Christian Ehrlicher

                                      @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?

                                      S Offline
                                      S Offline
                                      saeid0034
                                      wrote on last edited by
                                      #19
                                      This post is deleted!
                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        saeid0034
                                        wrote on last edited by
                                        #20

                                        ok i delete progressbar value after emit

                                        JonBJ 1 Reply Last reply
                                        0
                                        • S saeid0034

                                          ok i delete progressbar value after emit

                                          JonBJ Online
                                          JonBJ Online
                                          JonB
                                          wrote on last edited by
                                          #21

                                          @saeid0034
                                          That doesn't sound right, @Christian-Ehrlicher is referring to your HashChecker *hc = new HashChecker(); heap allocation needing to be deleted.

                                          1 Reply Last reply
                                          3

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved