Question

I must send a file through serial port, so I use a separate thread, named serialWorker.

In the main thread I have this:

ui->progressBar->setMaximum(file.size());    

QTextStream in(&file);
while(!in.atEnd()){

    serialWorker.send(line);  

    ui->progressBar->setValue(in.pos());        

}

The problem is that the progress bar is not "in real time", namely, as serialWorker doesn't block main thread, the loop runs without waiting. How I should approach this better?

Was it helpful?

Solution

You are updating progress bar in a loop which blocks main GUI thread thus your progressbar does not get updated.

You should use a signal in your serialWorker to notify the progress bar in your user interface of the value for the progress. In your send 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 send directly when it is in an other thread. The correct way is defining send as a slot and connecting a signal to that slot and emitting the signal when you want to call send.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top