Sometimes you need to call a very time-consuming api-class function in gui programming. If you do not use multithreading, the interface will become stuck. There is a very simple way to use multithreading. You do not need to create a new QThread derived class.
Set the api to be called
Bool demoDialog: threadDemo (QString realArgStr1, QString realArgStr2, int realArgInt)
{
////
...
////
}
If it is not a qt api function, just create a simple packaging function. Make sure that all the passed parameters are qt variables. Pointers cannot be passed here.
Call threadDemo in a function in the dialog box.
Export uture <bool> result = QtConcurrent: run (this, (& demoDialog: threadDemo), realArgStr1, realArgStr2, realArgInt );
While (result. isResultReadyAt (0 )! = True)
{
QCoreApplication: processEvents ();
}
If (result. result ())
{
// Success
}
Else
{
// Failed
}
You can run threadDemo () in the new thread without getting stuck. After threadDemo () is returned, the new thread automatically exits.
The disadvantage of this method is that the cpu usage during api operation is very high, which is caused by processEvents.