Run a timer based slot concurrently
-
Hi,
I have a slot calledmap_transfer(int init_map, float arr)
. I want to run this slot every 2 seconds and therefore, I have connected it to a timer. However, when I run this function, my entire GUI gets slowed down and gets stuck sometimes.
The UI gets stuck because this function saves an image locally whenever it is called.Therefore, I want to run this slot concurrently, so that the GUI doesn't get stuck.
How can I do so?
-
You have many options:
- have a QThread waiting for the job and trigger it's slot every 2 seconds using your timer
- use QtConcurrent
- use QtPromise
-
@sierdzio
Thanks for your reply. I want to use QtConcurrent. But unable to understand how to make it work for my situation.I have the following:
mytimer = new QTimer(this); connect(mytimer, SIGNAL(timeout()), this, SLOT(update_info())); timer->start(2000);
The update_info() slot has the function
map_transfer(int init_map, float arr)
(the time consuming function):void MainWindow::update_info() { QString str = this->mypath; QByteArray backTrack = str.toLocal8Bit(); char* mySpace = backTrack.data(); int success =map_transfer(init_map, arr); //time consuming function to be run concurrently int myRet = transfer_to_file(str, arr); }
the
transfer_to_file
function has to run after themap_transfer
function has completed its run. -
@sierdzio
Thanks for your reply. I want to use QtConcurrent. But unable to understand how to make it work for my situation.I have the following:
mytimer = new QTimer(this); connect(mytimer, SIGNAL(timeout()), this, SLOT(update_info())); timer->start(2000);
The update_info() slot has the function
map_transfer(int init_map, float arr)
(the time consuming function):void MainWindow::update_info() { QString str = this->mypath; QByteArray backTrack = str.toLocal8Bit(); char* mySpace = backTrack.data(); int success =map_transfer(init_map, arr); //time consuming function to be run concurrently int myRet = transfer_to_file(str, arr); }
the
transfer_to_file
function has to run after themap_transfer
function has completed its run.@BigBen
https://github.com/DeiVadder/QtThreadExampleand direct link to the concurrent subproject:
https://github.com/DeiVadder/QtThreadExample/tree/master/projects/QtConcurrent