A simple method to use qt Multithreading

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.