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.
  • 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 Offline
        JonBJ Offline
        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 Offline
            JonBJ Offline
            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 Offline
                          JonBJ Offline
                          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
                          • S Offline
                            S Offline
                            saeid0034
                            wrote on last edited by
                            #22

                            thanks, i get it

                            1 Reply Last reply
                            0

                            • Login

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