I have function, export()
in my QFileSystemModel
derived class. Now I want to show progress bar as this function does it work. I obvioulsy don't want to popup QProgressDialog
from there since GUI should be seperate.
void MainWindow::on_pushButtonConvert_clicked()
{
QString rootPath = ui->lineEditSourceFolder->text();
QString destPath = ui->lineEditDestFolder->text();
dirModel->convert(rootPath, destPath); // dirModel is QFileSystemModel derived member variable
}
Before moving convert()
to model, it was in my MainWindow
class. This function itself was creating QProgressDialog
but now after moving to mode, it should be prohibited from creating it so where do I create the progress?
I got a hint from another post that I should be using signal and slots but how here?
You should move dirModel
to a new thread in order to prevent export()
from blocking main thread and the UI. This can be done like:
QThread * th = new QThread();
dirModel->moveToThread(th);
QObject::connect(th,SIGNAL(started()),dirModel,SLOT(OnStarted()));
QObject::connect(th,SIGNAL(finished()),dirModel,SLOT(OnFinished()));
th->start();
Your initialization and termination tasks in dirModel
should be done in OnStarted()
and OnFinished()
slots respectively.
You should use a signal in your class to notify the progress bar in the user interface of the value for the progress. In your export()
function you should emit the signal with the appropriate value. The signal is like:
void progressChanged(int val);
You should also connect the progressChanged(int)
signal to the setValue(int value)
slot of the QProgressBar
.
And the last point is that you should not call export()
directly when it is in an other thread. The correct way is defining export()
as a slot and connecting a signal to that slot and emitting the signal when you want to call export()
.