How to pause and resume a progress bar in Qt 4.7
-
you set the progress of the progressbar with setValue() right? if your copy process pauses it implicitly also pauses the progressbar then ... or do i misunderstand something here?
-
Thanks for your response soon,
my snippet code:
@for(int i = 1; i <= totalFiles; i++){
ui->progressBar->setValue(i);
QString file = qsl.at(i-1);
QFileInfo finfo(QString("%1/%2").arg(src->path()).arg(file));
ui->lbl_CurrentFont->setText(finfo.fileName());
QFile::copy(finfo.filePath(),des->path()+ "/" + finfo.fileName());
ui->lbl_FontRemaining->setText(QString("%1 fonts remaining").arg((totalFiles - i)));
}@I don't know how to pause for loop and resume also.
What do us need to catch user click pause button. e.g: TimerEvent...
Thank you again.
An. -
well... this is way more complicated then. You can only achieve what you want with threads and a busy loop.
A nicer solution would be to hold 2 QFile objects and read/write chunks.
Here a very minimalistic example (without error handling) to give you an idea:
@
MyCopyClass::MyCopyClass()
{
m_ReaderFile.setFileName("C:/read.txt");
m_ReaderFile.open(QIODevice::ReadOnly);m_WriterFile.setFileName("C:/write.txt"); m_WriterFile.open(QIODevice::WriteOnly);
}
void MyCopyClass::pause() //SLOT
{
m_CopyPaused = true;
}void MyCopyClass::resume() //SLOT
{
if( m_CopyPaused )
{
m_CopyPaused = false;
this->copy();
}
}void MyCopyClass::copy() //SLOT
{
if( m_CopyPaused )
return;QByteArray chunk = m_ReaderFile.read(1024); //QFile m_WriterFile.write(chunk, 1024); //set progress of progressbar QTimer::singleShot(0, this, SLOT(copy()));
}
@Nevertheless i would execute every copy (this class) in an own thread.
You can connect your buttons to the pause/resume slots but ensure that you use a Qt::QueuedConnection when using threads. -
Hi, your example is copy content file to new file but my situation is copy all files in a folder, i think it same together. by the way, please tell me resolve if user's to destroy copy progressing, all file copied before will be rollback. how to do that?
as i know, we have a QDir.reomove(filename) function to do that, true or not?
I try to use it but not working.
Thanks a lot.
An. -
I think a thread would be a good choice in this situation. Signals and slots are your best friend.
As the thread copy is happening you can fire a signal in your loop that updates the progress bar in your main GUI telling it where it should be. You could even implement are "start" and "finished" signal, so when the copy begins, "start" should show your progress bar and initialize it to a specific value, get updates in-between while the copy is happening, then the "finished" signal would show some text say it has completed a 100%.
Or just one signal would suffice with 2 arguments, a command, and a value.
-
[quote author="phamvanan" date="1370622205"]Hi, your example is copy content file to new file but my situation is copy all files in a folder, i think it same together. by the way, please tell me resolve if user's to destroy copy progressing, all file copied before will be rollback. how to do that?
as i know, we have a QDir.reomove(filename) function to do that, true or not?
[/quote]the concept will be the same. You will get one folder for input and one folder for output. Now you can read all the files with "QDir::entryList()":http://qt-project.org/doc/qt-4.8/qdir.html#entryList-2 and do the copying like i exampled.
For undoing the files already copied you just need to store a list of the files you have already written and just delete them when you abort the copying before it's finished.[quote author="phamvanan" date="1370622205"]I try to use it but not working.[/quote]
What exactly is not working?!
You need to specify a file as parameter not a folder. So you will need to the recursion within a loop.You haven't chosen the right application to start programming in my opinion since it has some complexity in it and it needs some class design skills. But you leanr the most out of errors made ;)