1

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

             }
selbie
  • 100,020
  • 15
  • 103
  • 173
Aquiles
  • 13
  • 3
  • 1
    What is this? `SIGNAL(Value(int value))` – ixSci Apr 06 '22 at 05:35
  • 1
    Check [this Q&A](https://stackoverflow.com/questions/23438044/how-to-communicate-a-progresstext-from-a-qtconcurrentrun-function-or-similar), your code (because of your assumptions) is wrong in many places. – ixSci Apr 06 '22 at 05:52
  • Do you not see any error/warning messages at the console? [`QFutureWatcher`](https://doc.qt.io/qt-6/qfuturewatcher.html#signals) has no signal named `Value`. If you use the new signal/slot syntax mistakes such as this will be caught by the compiler. – G.M. Apr 06 '22 at 08:24
  • @G.M. if using macro `SIGNAL`, there won't be such warning. That's why after Qt5 using `&class::function` is suggested instead of using `SIGNAL(function)` – Louis Go Apr 06 '22 at 08:32
  • 1
    @LouisGo Using the `SIGNAL` macro should result in a runtime warning being output on the console. That's the message to which I was referring. – G.M. Apr 06 '22 at 08:35

0 Answers0