PDA

View Full Version : Using a QProgressbar in a For Loop



kiboi
20th October 2012, 03:13
Hi! I want to use a Qprogressbar using a For Loop. Every time the value inside the for loop changes, it sets the value of the progress bar. A sample code will be helpful.


Thanks!

wysota
20th October 2012, 08:56
for(int i=0;i<100;++i) { progressBar->setValue(i); qApp->processEvents(); }

kiboi
22nd October 2012, 07:43
thanks wysota. regarding process events, i tried it now, but it seems the process becomes slow. is there a way to adjust the process time? the process im doing is transferring of files.
Thanks!

wysota
22nd October 2012, 08:16
thanks wysota. regarding process events, i tried it now, but it seems the process becomes slow.
Obviously as your application has much more to do.


is there a way to adjust the process time? the process im doing is transferring of files.

e.g.

for(int i=0;i<100;++i) { progressBar->setValue(i); if(i % 100 == 0) qApp->processEvents(); }

kiboi
22nd October 2012, 09:13
my concern is that i want to prevent the application to get into "not responding" status if seen at the task manager while doing the for loop. i'll try your suggestion.thanks!

wysota
22nd October 2012, 09:19
You can always copy files in a secondary thread, e.g. something like this might work:


struct FileStruct {
QString sourcePath;
QString destPath;
FileStruct(s, d) : sourcePath(s), destPath(d) {}
};

bool copyFile(FileStruct s) {
QFile::copy(s.sourcePath, s.destPath);
return true;
}

QList<FileStruct> filesToCopy;
filesToCopy << FileStruct(...,...) << FileStruct(...,...) << FileStruct(...,...);

QFuture<bool> result = QtConcurrent::mapped(filesToCopy, copyFile);

but it will probably be slower than copying one file at a time (unless you have a SSD disk).