Answer the question
In order to leave comments, you need to log in
How to correctly send a function to a thread?
Hello.
there is a simple function for example:
void MainW::foo(){
if(ui->progressBar->value()<50){
ui->progressBar->setValue(79);
}
else{
ui->progressBar->setValue(23);
}
}
void MainW::on_output_excel_butt_clicked()
{
QThread trd;
moveToThread(&trd);
connect(&trd, SIGNAL(started()), this, SLOT(foo()));
trd.start();
}
Answer the question
In order to leave comments, you need to log in
Just in case, I'll leave this code here. As I understand it, this is what you want to get: a function in a separate thread, without blocking the GUI + you need to wait for the execution results.
QEventLoop loop;
QFutureWatcher watcher = QConcurrent::run(someFunc);
connect(&watcher, &QFutureWatcher::finished, &loop, &QEventLoop::quit);
loop.exec();
auto result = watcher.result();
EMNIP in Qt can only work with GUIs in the main thread. Those. some "heavy" or blocking actions should be done in a separate thread, and then, through signals, pull the functions in the main thread so that they change the state of the guys.
Try this option
connect(&trd, SIGNAL(started()), this, SLOT(foo()), Qt::QueuedConnection);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question