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. ProgressBar for File Copy from source to destination on QT 5.9.2

ProgressBar for File Copy from source to destination on QT 5.9.2

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.0k Views
  • 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.
  • R Offline
    R Offline
    Raghavendra Surpur
    wrote on last edited by
    #1

    Hi All,

    We are working on QT 5.9.2 for GUI (QtQucik Control Application 2)application, in that i want to work on progressbar , for my application there is a one button , when i clicked that button one more screen is appear in that it showing perentage of file copy(how many bytes file is copying), So Please give me any suggestions or any examples.

    Thanks,
    Raghavendra

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      QFile does not report progress of file copying. So you need to either use a different library or copy file contents manually: one QFile to read from, another QFile to read to. That would give you a nice loop in which you can send percentage of completion to your progress bar easily (using some signal).

      (Z(:^

      R 1 Reply Last reply
      5
      • R Offline
        R Offline
        Raghavendra Surpur
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • sierdzioS sierdzio

          QFile does not report progress of file copying. So you need to either use a different library or copy file contents manually: one QFile to read from, another QFile to read to. That would give you a nice loop in which you can send percentage of completion to your progress bar easily (using some signal).

          R Offline
          R Offline
          Raghavendra Surpur
          wrote on last edited by
          #4

          @sierdzio
          Please send me any examples on that

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5
            QFile in("input.txt");
            QFile out("output.txt");
            
            // I omit obvious stuff like checking if files exist, can be read/ written to, opening the files etc.
            
            for (int i  = 0; i < in.length(); ++i) {
                const QByteArray line = in.readLine();
                out.write(line);
                emit copyProgress(i, line);
            }
            

            Something like this. For a more robust solution, use QDataStream and don't read lines but rather chunks of data. QFile documentation will help you with that.

            (Z(:^

            R 1 Reply Last reply
            3
            • sierdzioS sierdzio
              QFile in("input.txt");
              QFile out("output.txt");
              
              // I omit obvious stuff like checking if files exist, can be read/ written to, opening the files etc.
              
              for (int i  = 0; i < in.length(); ++i) {
                  const QByteArray line = in.readLine();
                  out.write(line);
                  emit copyProgress(i, line);
              }
              

              Something like this. For a more robust solution, use QDataStream and don't read lines but rather chunks of data. QFile documentation will help you with that.

              R Offline
              R Offline
              Raghavendra Surpur
              wrote on last edited by
              #6

              @sierdzio
              Thanks sierdzio

              Will try this

              1 Reply Last reply
              0
              • J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                this is of course untested and from the top of my head, no guarantee of compile succes or efficiency ;-)

                 QProgressBar *progress = new QProgessBar();
                
                QtConcurrent::run(this, [=]{
                
                QFile src(SourceFile);
                QFile dst(DestinationFile);
                
                int chunkSize(256);
                
                if(src.exits() && src.open(QIODevice::ReadOnly) ){
                     QByteArray dataToCopy = src.readAll();
                     if(dst.open(QIODevice::WriteOnly) {
                          connect(this, myClass::setMaximum, progress, &QProgressBar::setMaximum);
                          connect(this, myClass::setValue, progress, &QProgressBar::setValue);
                
                         emit setMaximum(src.size() / chunkSize);
                
                         int index(0);
                         while(!src.etEnd()){
                                QByteArray data = src.read(chunkSize);
                                dst.write(data);
                                index++;
                                emit setValue(index);
                         }         
                   }
                }
                   progress->deleteLater();
                };
                
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                2

                • Login

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