ProgressBar for File Copy from source to destination on QT 5.9.2



  • 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


  • Moderators

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



  • This post is deleted!


  • @sierdzio
    Please send me any examples on that


  • Moderators

    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.



  • @sierdzio
    Thanks sierdzio

    Will try this



  • 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();
    };
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.