how to use signals and slots with QtConcurrent::run and QFutureWatcher

502 views Asked by At

I'm trying to copy files with the progress bar, I have a method that works with QtConcurrent::run but when I connect to progressBar nothing happens but the file is copied successfully. I would like to know what is wrong with my code and how to fix that thank you below we have my copy method and how I connect signals and slots.

QProgressDialog       progressDialog;
QFutureWatcher<void>  futureWatcher;
QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int,int)), &progressDialog, SLOT(setRange(int,int)));
QObject::connect(&futureWatcher, SIGNAL(Value(int value)), &progressDialog, SLOT(setValue(int)));
QObject::connect(&futureWatcher, &QFutureWatcher<void>::finished, &futureWatcher, &QFutureWatcher<void>::deleteLater);
QFuture<void>  future = QtConcurrent::run([&, this, listu]() -> void
{
    copia(listu.at(i), desPath);
});

futureWatcher.setFuture(future);
progressDialog.exec();
futureWatcher.waitForFinished();

the copy method

void BackgroundContextMenu::copia(QString src,QString dest)
{
//QFile::copy(src,dest);
QFile Src(src);
QFile dst(dest);

      int chunkSize(256);

      if(Src.exists() && Src.open(QIODevice::ReadOnly) ){
           QByteArray dataToCopy = Src.readAll();
           if(dst.open(QIODevice::WriteOnly)) {
              
               int index(0);
               while(!Src.atEnd()){
                      QByteArray data = Src.read(chunkSize);
                      dst.write(data);
                      index+=chunkSize;
                    emit Value(index*100/Src.size());//current file copy progress


               }
               Src.close();
               dst.close();

             }
0

There are 0 answers