문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top