PDA

View Full Version : GUI gets hang while copying data from one file to another



anh5kor
4th February 2016, 09:07
I have GUI in which on button click copy the content from one file to another file.While copying my GUI get hangs and it get release once the copying is done.
I tried to implement QtConcurrent function in my program but still GUI hangs for couple of second.
My code


connect(ui->button1,SIGNAL(clicked()),this,SLOT(select_C_file( )));
void MainWindow::select_C_file()
{
QDir directory("Documents");
QString path = directory.filePath(" ");
QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"),path,tr("(*.lst)"));//open the file path which has to copy

//Thread implementation
QFuture<void> future = QtConcurrent::run(this,&MainWindow::openCfile,QString(fileName));
future.waitForFinished();

void MainWindow::openCfile(QString fileName)
{
//program to copy
}

}

My program is working perfectly but still my GUI is getting hang.
Please let me how can I use Qthread instead of QtConcurrent.Where my Qthread should start on the button click and should end once the copying is done or it should be in threadloop.

anda_skoa
4th February 2016, 10:31
I tried to implement QtConcurrent function in my program but still GUI hangs for couple of second.

Because you are calling waitForFinished()



Please let me how can I use Qthread instead of QtConcurrent
You could first try not to block the main thread by not calling waitForFinished()

Cheers,
_

anh5kor
4th February 2016, 11:03
Thankyou its working

anda_skoa
4th February 2016, 13:34
If you want to get notified when the future is completed, have a look at QFutureWatcher.

Cheers,
_